Import Socketdef handle_client (Socket_con): "" "receives the request from the client and receives the request message, parses, returns" "" # server receives the client's request message requests = S OCKET_CON.RECV (4096). Decode () print (request) # Server splicing response message and reply Response_line = ' http/1.1 ok\r\n ' Response_hea d = ' Server:skylark 2.0\r\n ' response_head + = ' content-type:text/html;charset=utf-8\r\n ' response_body = ' This is what is displayed!\r \ n ' response = response_line + Response_head + ' \ r \ n ' + response_body Socket_con.send (Response.encode ()) Socket_c On.close () def main (): # The server creates the socket Socket_listen = Socket.socket (socket.af_inet, socket) responsible for listening. SOCK_STREAM) # Set port reuse socket_listen.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR, True) # binds the Listening port socket_listen.bind ((' ', 8888)) # Set the listening queue Socket_listen.listen (128) # through the loop different accept please Ask while True: # accept () returns a tuple (socket object, address info) Socket_con, Socket_con_adds = Socket_listen.a Ccept () # Print a hint message in print (' Client: ', Socket_con_adds, ' Connect successfully! ') # using functions to handle client sendsInformation Handle_client (socket_con) if __name__ = = ' __main__ ': Main ()
Web server that returns fixed data