代码:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("localhost", 8000))
sock.listen(2)
while True:
fd, addr = sock.accept()
ls = fd.recv(65530)
print(ls)
fd.send(b"HTTP/1.1 200 OK\r\n\r\n<html><body>hello</body></html>")
sock.close()
print返回信息:
在浏览器输入locahost:8000,返回下面信息:
b'GET / HTTP/1.1\r\nHost: localhost:8000\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests:
1\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/73.0.3683.86 Safari/537.36\r\nAccept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.
8,application/signed-exchange;v=b3\r\nAccept-Encoding: gzip, deflate, br\r\nAccept-
Language: zh-CN,zh;q=0.9,en;q=0.8\r\nCookie: Pycharm-5642710=81a11ecb-fd3b-
4fa3-9a69-9fd19536a02c; _ga=GA1.1.961478357.1550867780\r\n\r\n'
b'GET /favicon.ico HTTP/1.1\r\nHost: localhost:8000...
在浏览器输入localhost:8000/hello, 返回以下信息。
b'GET /hello HTTP/1.1\r\nHost: localhost:8000\r\nConnection: keep-alive\r\nUpgrade-Insecure-
Requests: 1\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/73.0.3683.86 Safari/537.36\r\nAccept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/sig
ned-exchange;v=b3\r\nAccept-Encoding: gzip, deflate, br\r\nAccept-Language: zh-
CN,zh;q=0.9,en;q=0.8\r\nCookie: Pycharm-5642710=81a11ecb-fd3b-4fa3-9a69-9fd19536a02c;
_ga=GA1.1.961478357.1550867780\r\n\r\n'
b'GET /favicon.ico HTTP/1.1\r\nHost: localhost:8000\r\nConnection: keep-alive\r\nUser-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86
Safari/537.36\r\nAccept: image/webp,image/apng,image/*,*/*;q=0.8\r\nReferer:
**http://localhost:8000/hello**\r\nAccept-Encoding: gzip, deflate, br\r\nAccept-Language: zh-
CN,zh;q=0.9,en;q=0.8\r\nCookie: Pycharm-5642710=81a11ecb-fd3b-4fa3-9a69-9fd19536a02c;
_ga=GA1.1.961478357.1550867780\r\n\r\n'
第一次返回的两条信息路径都为:
http://localhost:8000/
第二次返回的两条信息,
一条为GET /hello ... http://localhost:8000/,
另一条为http://localhost:8000/hello
从这两条信息的不同, 就可以开发出web框架。加之异步编程,以及高效的算法,就可以开发出高性能的web框架。不用cgi不用wsgi。