一 socket简介
1 最简单的socket
#################server import socket ip_port = ('127.0.0.1',9997) sk = socket.socket() sk.bind(ip_port) sk.listen(5) print('server waiting...') conn,addr = sk.accept() client_data = conn.recv(1024) print(str(client_data,"utf8")) conn.sendall(bytes('滚蛋!',encoding="utf-8")) sk.close() ################client import socket ip_port = ('127.0.0.1',9997) sk = socket.socket() sk.connect(ip_port) sk.sendall(bytes('俺喜欢你',encoding="utf8")) server_reply = sk.recv(1024) print (str(server_reply,"utf8"))
上面主要就是服务器端启动,并监听端口等待客户端连接,客户端连接发送一个“俺喜欢你”,服务器回复“滚蛋”,这个是最简单的SOCKET的流程了。
2 socket的写一个简单的web应用
这里主要是使用浏览器模拟socket的客户端,server端自己实现。
# 对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端。 import socket def handle_request(client): buf = client.recv(1024) # 接收浏览器发送的内容 print(buf) client.send('HTTP/1.1 201 OK '.encode('gbk')) # 向浏览器发送包头状态码 client.send('<h1>你好,世界 ! </h1>'.encode('gbk')) # 向浏览器发送内容 def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost',8000)) sock.listen(5) while True: # 这个conn是client的socket对象,相当于client主动和server建立了一条通道 conn,add = sock.accept() handle_request(conn) conn.close() if __name__ == '__main__': main()
可以看到server端启动之后,会创建一个sock对象,然后绑定ip:port,进行监听浏览器的连接,这个先接收浏览器发送过来的内容,然后向浏览器发送一个包头,一个内容。这个包头浏览器可以自动识别。
所以这里可以看到web应用其实本质上是一个socket的server端,浏览器本质上是一个socket的client端。所有的其他都是基于这里来不断的进行丰富。
具体浏览器向我们server端发送了些什么
"E:softwarepython3.6install dirpython.exe" E:/workspace/django_flow/app01/tests.py b'GET / HTTP/1.1 Host: localhost:8000 Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, sdch, br Accept-Language: zh-CN,zh;q=0.8 HT-Ver: 1.1.2 HT-Sid: QR8okFW9-0F6VyaG0-VMYijoqf-eENDjuaW-qsT4qDpV-LWRAthtW-yqDF5U7/-vaL3Hjav ' b'GET /favicon.ico HTTP/1.1 Host: localhost:8000 Connection: keep-alive User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Accept: image/webp,image/*,*/*;q=0.8 Referer: http://localhost:8000/ Accept-Encoding: gzip, deflate, sdch, br Accept-Language: zh-CN,zh;q=0.8 HT-Ver: 1.1.2 HT-Sid: QR8okFW9-0F6VyaG0-VMYijoqf-eENDjuaW-qsT4qDpV-LWRAthtW-yqDF5U7/-vaL3Hjav '
二 wsgiref源码分析
from wsgiref.simple_server import make_server # python3默认str是unicode,这里需要bytes,所以需要encode转换一下 def application(environ,start_response): start_response('200 OK', [('Content-Type','text/html')]) return ['<h1> 你好,世界!</h1>'.encode('gbk')] httpd = make_server('localhost',8080,application) print('Saving HTTP on port 8080') httpd.serve_forever()
上面的代码运行之后就是一个web应用了。
下面对上面的代码进行分析。
1 首先执行 httpd = make_server('localhost',8080,application)
2 然后执行 httpd.serve_forever()
首先我们分析第一步,
def make_server( host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler ): """Create a new WSGI server listening on `host` and `port` for `app`""" # 这里先执行WSGIServer类的初始化工作,其中handler_class=WSGIRequestHandler server = server_class((host, port), handler_class) server.set_app(app) return server
首先,make_server是一个函数,接收host,port,app等参数。看样子这个函数返回的是一个server对象。也就是WSGIServer类的对象。
这里使用server_class进行了类的初始化,(host,port)是一个数组,handler_class= WSGIRequestHandler是一个类,然后执行server.set_app(app),这里的app就是上面的application。
server_class类
class WSGIServer(HTTPServer): """BaseHTTPServer that implements the Python WSGI protocol""" application = None def server_bind(self): """Override server_bind to store the server name.""" HTTPServer.server_bind(self) self.setup_environ() def setup_environ(self): # Set up base environment env = self.base_environ = {} env['SERVER_NAME'] = self.server_name env['GATEWAY_INTERFACE'] = 'CGI/1.1' env['SERVER_PORT'] = str(self.server_port) env['REMOTE_HOST']='' env['CONTENT_LENGTH']='' env['SCRIPT_NAME'] = '' def get_app(self): return self.application def set_app(self,application): self.application = application
先来分析server实例化的过程:跑题了
1)WSGIServer类并没有__init__(),所以需要到HTTPServer类中进行查找
这里关于继承有2中查找方法:
经典类:深度优先
新式类:广度优先
好了,补充完毕,至于神马是经典类,神马是新式类。继承了object的类叫新式类,不继承的那就是经典类。简单吧
2)查看HTTPSserver类
class HTTPServer(socketserver.TCPServer): allow_reuse_address = 1 # Seems to make sense in testing environment def server_bind(self): """Override server_bind to store the server name.""" socketserver.TCPServer.server_bind(self) host, port = self.server_address[:2] self.server_name = socket.getfqdn(host) self.server_port = port
这里类也没有__init__()方法,继续查看TCPServer吧。好吧这类TCPServer类不但有__init__(),还需要执行父类的__init__()
handler_class类
class WSGIRequestHandler(BaseHTTPRequestHandler): server_version = "WSGIServer/" + __version__ def get_environ(self): env = self.server.base_environ.copy() env['SERVER_PROTOCOL'] = self.request_version env['SERVER_SOFTWARE'] = self.server_version env['REQUEST_METHOD'] = self.command if '?' in self.path: path,query = self.path.split('?',1) else: path,query = self.path,'' env['PATH_INFO'] = urllib.parse.unquote(path, 'iso-8859-1') env['QUERY_STRING'] = query host = self.address_string() if host != self.client_address[0]: env['REMOTE_HOST'] = host env['REMOTE_ADDR'] = self.client_address[0] if self.headers.get('content-type') is None: env['CONTENT_TYPE'] = self.headers.get_content_type() else: env['CONTENT_TYPE'] = self.headers['content-type'] length = self.headers.get('content-length') if length: env['CONTENT_LENGTH'] = length for k, v in self.headers.items(): k=k.replace('-','_').upper(); v=v.strip() if k in env: continue # skip content length, type,etc. if 'HTTP_'+k in env: env['HTTP_'+k] += ','+v # comma-separate multiple headers else: env['HTTP_'+k] = v return env def get_stderr(self): return sys.stderr def handle(self): """Handle a single HTTP request""" self.raw_requestline = self.rfile.readline(65537) if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) return if not self.parse_request(): # An error code has been sent, just exit return handler = ServerHandler( self.rfile, self.wfile, self.get_stderr(), self.get_environ() ) handler.request_handler = self # backpointer for logging handler.run(self.server.get_app())