Written in Python using simple static Web server (TCP protocol)

Import Socket 

DEF service_client (Since the software of):
     # receiving client needs 
    Request = new_socket.recv (1024 )
     Print (Request)
     # return data to the client 
    Response = ' the HTTP / 1.1 200 is the OK \ R & lt \ n- ' 
    Response + = ' \ R & lt \ n- ' 
    Response + = ' <h1 of> Hello </ h1 of> ' 
    new_socket.send (response.encode ( ' UTF-. 8 ' )) 

DEF main ():
     # create socket 
    serve_socket = socket.socket (socket .AF_INET, socket.SOCK_STREAM)
     #Binding ip and port 
    serve_socket.bind (( '' , 7080 ))
     # the listen client needs 
    serve_socket.listen (128 )
     # waiting for clients to link the Accept 
    Since the software of, client_addr = serve_socket.accept ()
     Print (Since the software of)
     Print (client_addr)
     # services client recv and the send 
    service_client (Since the software of) 


IF  __name__ == ' __main__ ' : 
    main ()

As with the basic ideas of creation methods tcp server

Create a socket with the socket → bind with binding request address (ip and port) → listen listening client → accept links → wait for the client service clients (mainly demand recv send data)

Wherein serve_socket.accept () returns the data to a socket and a client address (ip and port), recv send demand data is processed by the returned socket

 

Guess you like

Origin www.cnblogs.com/cokefentas/p/11106106.html