Python network programming test-host response

Source: Internet
Author: User

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

 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.