web框架_socket连接

# __author: "ZXYang"
# date: 2020/9/14

import socket


def handle_request(client):
buf = client.recv(1024)
client.send("HTTP/1.1 200 OK\r\n\r\n".encode("utf8"))

# client.send("<h1 style='color:red'>Hello, yuan</h1>".encode("utf8"))
with open("index01.html", 'rb')as f:
data = f.read()
client.send(data)


def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 8001))
sock.listen(5)

while True:
connection, address = sock.accept()
handle_request(connection)
connection.close()


if __name__ == '__main__':
main()
posted @ 2020-09-16 21:00  zxy_ang  阅读(196)  评论(0)    收藏  举报