On the last day of the May Day holiday, write the code at will. First, the last small server responds to the UDP request.
Because I only have one machine, I use the lo0 port and set the socket to SO_REUSEADDR.
Server:
[Python] # test of host
Import sys, socket, traceback, time
Host =''
Xport = sys. argv [1]
Try:
Port = int (xport)
Failed t ValueError:
Port = socket. getservbyname (xport, 'udp ')
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
S. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1)
S. bind (host, port ))
Print ('Waiting for a client ...')
While True:
Try:
Message, address = s. recvfrom (1024)
If (message = 'time \ n '):
Print ('get a client request for time ...')
S. sendto ('reply: '+ time. ctime (), address)
Elif (message = 'Hi \ n') or (message = 'Hello \ n ')):
Print ('get a client from: ', address)
S. sendto ('reply: hi client', address)
Else:
Print ('get a client from: ', address)
S. sendto ('What wocould you need? ', Address)
Except T (KeyboardInterrupt, SystemExit ):
Raise
Except t:
Traceback. print_exc ()
# Test of host
Import sys, socket, traceback, time
Host =''
Xport = sys. argv [1]
Try:
Port = int (xport)
Failed t ValueError:
Port = socket. getservbyname (xport, 'udp ')
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
S. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1)
S. bind (host, port ))
Print ('Waiting for a client ...')
While True:
Try:
Message, address = s. recvfrom (1024)
If (message = 'time \ n '):
Print ('get a client request for time ...')
S. sendto ('reply: '+ time. ctime (), address)
Elif (message = 'Hi \ n') or (message = 'Hello \ n ')):
Print ('get a client from: ', address)
S. sendto ('reply: hi client', address)
Else:
Print ('get a client from: ', address)
S. sendto ('What wocould you need? ', Address)
Except T (KeyboardInterrupt, SystemExit ):
Raise
Except t:
Traceback. print_exc ()
Client:
[Python] # test of udp
Import sys, socket
Host = sys. argv [1]
Xport = sys. argv [2]
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
Try:
Port = int (xport)
Failed t ValueError:
Port = socket. getservbyname (xport, 'udp ')
Try:
S. connect (host, port ))
Print ('input the data ')
Data = sys. stdin. readline ()
S. sendall (data)
Failed t socket. error, e:
# Test of udp
Import sys, socket
Host = sys. argv [1]
Xport = sys. argv [2]
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
Try:
Port = int (xport)
Failed t ValueError:
Port = socket. getservbyname (xport, 'udp ')
Try:
S. connect (host, port ))
Print ('input the data ')
Data = sys. stdin. readline ()
S. sendall (data)
Failed t socket. error, e:
Start two terminals for testing:
The client inputs and obtains the Server Response (UDP ):
Server display:
OK. There are some summary at the end.
1. KeyboardInterrupt interrupt processing. The solution on win32 is different from that on Linux or UNIX.
2 use try... finally code block to close socket
3. Tips for using Python shell to query socket options (it feels a bit like SQL)
From the column of FishinLab