from socket Import * Import socket DEF main (): # create a tcp socket TCP_SOCKET = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # linked server # tcp_socket.connect (( "10.6.25.79", 8080)) server_ip the iNPUT = ( " Please enter the need to link the server IP: " ) SERVER_PORT = int (the iNPUT ( " Please enter the need to link the server Port: " )) server_addr = (server_ip, SERVER_PORT) tcp_socket.connect (server_addr) # sending data send_data = the iNPUT ( " Please enter the data sent:") tcp_socket.send(send_data.encode("gbk")) # 关闭套接字 tcp_socket.close() if __name__ == "__main__": main()
from socket Import * Import socket DEF main (): # buy a cell phone - create tcp socket TCP_SOCKET = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # into the phone card - binding tcp_socket.bind (( "" , 7890 )) # set the phone to ring the normal mode (the default socket so that by the active becomes passive and the listen) tcp_socket.listen (128 ) Print ( " ----- 1 ----- " ) # waiting for the arrival of someone else's phone (waiting for a client link the Accept) client_socket, client_addr = tcp_socket.accept () Print ( " ----- 2 ----- ") # Print (client_socket) Print (client_addr) # receiving a request sent by the client recv_data = client_socket.recv (1024 ) Print (recv_data.decode ( " GBK " )) # loopback portion of the data to the client client_socket.send ( " ha " .encode ( " GBK " )) # close the socket tcp_socket.close () client_socket.close () IF __name__ == " __main__ " : main ()
from socket Import * Import socket DEF main (): # buy a cell phone - create tcp socket TCP_SOCKET = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # into the phone card - binding tcp_socket.bind (( "" , 7890 )) # set the phone to ring the normal mode (the default socket so that by the active becomes passive and the listen) tcp_socket.listen (128 ) the while True: Print ( " wait for the arrival of a new client " ) # wait the arrival of someone else's phone (waiting for a client link the Accept) client_socket, client_addr = tcp_socket.accept () #Print (client_socket) Print ( " a new client to the S% " % STR (client_addr)) # sent by a client to request recv_data = client_socket.recv (1024 ) Print ( " client request was sent,:% S " % recv_data.decode ( " GBK " )) # loopback part of the data to the client client_socket.send ( " ha ha " .encode ( " GBK " )) # close the socket # close the socket accept returns, meaning no longer serve as the client client_socket.close () Print ( "Services have been completed " ) # If the listening socket is closed, there will lead to not wait for the arrival of new clients once again, that XXX.accept will fail tcp_socket.close () IF __name__ == " __main__ " : main ()
from socket Import * Import socket DEF main (): # buy a cell phone - create tcp socket TCP_SOCKET = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # into the phone card - binding tcp_socket.bind (( "" , 7890 )) # set the phone to ring the normal mode (the default socket so that by the active becomes passive and the listen) tcp_socket.listen (128 ) the while True: # cycle for multiple client service Print ( " wait for a new the arrival of the client " ) # wait for the arrival of someone else's phone (wait for the client link the Accept) client_socket, client_addr = tcp_socket.accept () # Print (client_socket) Print ( " a new client to the S% " % STR (client_addr)) the while True: # cycle times for the same service client # request sent by a client to the recv_data = client_socket.recv ( 1024 ) Print ( " client requests are sent,:% S " % recv_data.decode ( " GBK " )) # If clogging recv solution, two embodiment # 1 sent by the client data # 2 led to a client calls close recev de-clog IF recv_data: # echo part of the data to the client client_socket.send (" Ha ha " .encode ( " GBK " )) the else : BREAK # close the socket # close the socket accept returns, meaning that the client will not be for this service client_socket.close () Print ( " has been completed service " ) # If the listening socket is closed, there will lead to not wait for the arrival of new clients once again, that XXX.accept will fail tcp_socket.close () IF __name__ == " __main__ " : main ()