所有的web框架到头来都是这些核新代码:
#_*_coding:utf-8_*_
# Author:
import socket
# 处理函数
def handle_request(client):
buf = client.recv(1024)
client.send("HTTP/1.1 200 OK \r\n\r\n".encode('utf-8'))
client.send("Hello, severn".encode('utf-8'))
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 8008)) # 端口可以自定义
sock.listen(5)
while True:
connection, address = sock.accept()
handle_request(connection)
connection.close()
if __name__ == '__main__':
main()