Python3 socket Web编程

代码:

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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值