This article mainly introduces the python network programming example and provides the client and server. if you need it, refer to the client.
The code is as follows:
If _ name _ = '_ main __':
Import socket
Import OS
Sock = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Sock. connect ("localhost", 8001 ))
Import time
Time. sleep (2)
Sock. send ('1 ')
Print sock. recv (1024)
# OS. system ('pause ')
# Raw_input ()
Time. sleep (3)
Sock. close ()
Server
The code is as follows:
If _ name _ = '_ main __':
Import socket
Sock = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Sock. bind ('localhost', 8001 ))
Sock. listen (5)
While True:
Connection, address = sock. accept ()
Try:
Connection. settimeout (5)
Buf = connection. recv (1024)
If buf = '1 ':
Connection. send ('welcome to server! ')
Else:
Connection. send ('Please go out! ')
Failed T socket. timeout:
Print 'time out'
Connection. close ()