1 # !/usr/bin/env python
 2 # coding:utf-8
 3 import socket
 4 
 5 
 6 def handle_request(client):
 7     buf = client.recv(1024)
 8     client.send("HTTP/1.1 200 OK\r\n\r\n")#状态码
    #client.send("Content-Type:text/html\r\n\r\n")#页面显示的格式
9 client.send("<a href='http://www.baidu.com'>Hello, World</a>") 10 11 12 def main(): 13 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 14 sock.bind(('localhost', 8080)) 15 sock.listen(5) 16 17 while True: 18 connection, address = sock.accept() 19 handle_request(connection)#当服务器接受请求并返回数据之后就断开连接不会出现交互的模式 20 connection.close() 21 22 23 if __name__ == '__main__': 24 main()

 

相关文章:

  • 2022-12-23
  • 2021-07-11
  • 2021-11-04
  • 2021-07-15
  • 2021-06-04
  • 2021-04-01
  • 2021-11-08
猜你喜欢
  • 2021-09-09
  • 2021-12-23
  • 2021-10-28
相关资源
相似解决方案