HTTP Socket Network communication

Source: Internet
Author: User

  1 Import Socket 2 3 class WebServer (): 4 5 def __init__ (self): 6 "' 7 1. Create a total socket 8 2. Listen 9:p Aram Self:10:p Aram Addr:11:return:12 "13 # Create Socket Soncket What is the essence of a function? Self.skt = Socket.socket (socket.af_inet, socket.         SOCK_STREAM) 15 16 # Bind a port and IP address addr = (' 127.0.0.1 ', 7866) self.skt.bind (addr) 19 20 # continuous listening go to Last Call Kaishi Self.skt.listen () Kaishi def (self): #循环, in order for everything sent over to receive I have a question ? While true:26 #浏览器的信号或者请求报文从这里开始接收 to In_skt there is an address exist in_addr in_skt,in_addr = self.             Skt.accept () #实例化对象以及触发SocketHandler类的init函数 go to the init function self.sh = Sockethandler (in_skt) 30 #调用函数chuli return the Last Call to Self.sh.chuli () class Sockethandler (object): 34 "35 handles all  Connection of a single incoming socket "PNs Chuli (self): 38" ' 39       Processing incoming socket Connections 40 There are three main tasks 41 1. Receive the request and make a Judgment 42 2. Send Feedback 43 3. Close Request 44:return:45 "#调用方法 self.printhttp () #调用函数 parsing http self.parsehttp () #调         Using the Print dictionary method this request is the requested meaning self.printrequest () #调用方法 router is the meaning of the router self.router () 54  #skt is the channel, this is the end, turn off return Kaishi self.skt.close () #响应方法 server sent the Sendback (Self,url): 58 " 59 returns information in the specified format 60:return:61 "" #在外面声明一个空的字符串 + HTML = "#读取url文 Content inside the contents of the If url = = '/': "With open ('./index.html ', ' R ') as fp:67 html = fp.read () 6                     8 else:69 with open (URL, ' R ') as f:70 #读取的内容存取到html HTML is string type 71 html = F.read () #声明一个分数的字典 is used to traverse the score = {' Liu Chong ': 99, ' Lao Wang ': 33, ' Lao Li ': ' The ', ' #声明            An empty string of content 75 Content = "#items () the function of the dictionary score the key value into a tuple-like form to facilitate traversal of types that return similar tuples #遍历 the key values are traversed separately into the empty k,v 78 For k,v in Score.items (): #字符串格式化 and pass the k,v in Content_tpl what is the meaning of this variable name, ask God to help??????????? CONTENT_TPL = "

  

HTTP Socket Network communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.