wgsi实现了socket-server功能

#_author:来童星
#date:2020/2/21
import socket

def handle_request(client):
#将发送过来的信息拿到
buf=client.recv(1024)
#服务器的响应信息
client.send("HTTP/1.1 200 ok\r\n\r\n".encode('utf-8'))
with open('index.html','rb')as f:
data=f.read()
client.send(data)
#服务器将下面信息发送给浏览器,让浏览器渲染
# client.sent("<h1 style='color:red'>hello star</h1>").encode('utf-8')

def main():
#main函数一旦执行,会创建一个sockek对象
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('',8001))
sock.listen(5)


while True:
#如果浏览器没有发送请求,会一直卡在这里
connection,address=sock.accept()
print('port 8001 is running')
#处理请求
handle_request(connection)
connection.close()
if __name__=='__main__':
main()
posted @ 2020-02-21 11:55  Stary_tx  阅读(195)  评论(0编辑  收藏  举报