1.建立一个TCP服务器
2.浏览器发送请求request
3.服务器发送response给浏览器
服务器代码如下:

import socket
 
 
def service_client(new_socket):
    """为这个客户返回数据"""
    #  1.接收浏览器发送过来的请求,即HTTP请求
    #  GET / HTTP/1.1
    request=new_socket.recv(1024)
    print(request)
 
    #  2.返回HTTP格式的数据,给浏览器
    #  准备发送的header
    response="HTTP/1.1 200 OK\r\n"
    response+="\r\n"  #  header与body之间必须隔一行
    #  准备发送的body
    response+=""
    new_socket.send(response.encode("utf-8"))
 
    new_socket.close()
 
def main():
    tcp_sever_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    #  服务器先关闭,保证重新开启不占用端口
    tcp_sever_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    tcp_sever_socket.bind(("",7890))
    tcp_sever_socket.listen(128)
    while True:
        #  等待新客户端的链接
        new_socket,client_addr=tcp_sever_socket.accept()
        service_client(new_socket)
    #  关闭监听套接字
    tcp_sever_socket.close()
if __name__ == '__main__':
    main()