Python network programming test-broadcast

Source: Internet
Author: User

If I haven't logged on for a few days, I always feel that I have less things (because I caught a thief). Today I want to talk about the small test recently. It seems that I can change it on a large project in the future.

1 broadcast: (IPv4 and no v6 are used in the following scenarios. Of course, no v5 is used)

Broadcast is very important for routing. The network knowledge involved is not described here. First, let's look at the code and the processing of broadcast in Python.

Host: (receives broadcast packets and responds)

[Python] def hostBroadcastRecv ():
Host =''
Print ('input the port number for recieving broadcast data :')
Port = sys. stdin. readline (). rstrip ()
 
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
S. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1)
S. setsockopt (socket. SOL_SOCKET, socket. SO_BROADCAST, 1)
S. bind (host, int (port )))
 
While True:
Try:
Mess, addr = s. recvfrom (1024)
Print ('got data from broadcast ')
S. sendto ('got your broadcast', addr)
 
Except T (KeyboardInterrupt, SystemExit ):
Print ('terminate the procedure? ')
Flag = sys. stdin. readline (). rstrip ()
If (flag = 'yes') or (flag = 'y '):
Sys. exit (1)
Else:
Continue
Except t:
Traceback. print_exc ()
Def hostBroadcastRecv ():
Host =''
Print ('input the port number for recieving broadcast data :')
Port = sys. stdin. readline (). rstrip ()

S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
S. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1)
S. setsockopt (socket. SOL_SOCKET, socket. SO_BROADCAST, 1)
S. bind (host, int (port )))

While True:
Try:
Mess, addr = s. recvfrom (1024)
Print ('got data from broadcast ')
S. sendto ('got your broadcast', addr)

Except T (KeyboardInterrupt, SystemExit ):
Print ('terminate the procedure? ')
Flag = sys. stdin. readline (). rstrip ()
If (flag = 'yes') or (flag = 'y '):
Sys. exit (1)
Else:
Continue
Except t:
Traceback. print_exc ()
Client: (send broadcast packets)

[Python] def sendBroadcastData ():
Print ('input the data for sending ...')
Data = sys. stdin. readline (). rstrip ()

Try:
Dest = ('<broadcast>', 5522)
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
S. setsockopt (socket. SOL_SOCKET, socket. SO_BROADCAST, 1)
 
S. sendto ('testing... ', dest)
 
Mess, addr = s. recvfrom (1024)
Print (str (mess ))
Except T (KeyboardInterrupt, SystemExit ):
Print ('terminate the procedure? ')
Flag = sys. stdin. readline (). rstrip ()
If (flag = 'yes') or (flag = 'y ')):
Sys. exit (1)
Else:
Continue
Except t:
Sys. exit (1)
Def sendBroadcastData ():
Print ('input the data for sending ...')
Data = sys. stdin. readline (). rstrip ()
 
Try:
Dest = ('<broadcast>', 5522)
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
S. setsockopt (socket. SOL_SOCKET, socket. SO_BROADCAST, 1)

S. sendto ('testing... ', dest)

Mess, addr = s. recvfrom (1024)
Print (str (mess ))
Except T (KeyboardInterrupt, SystemExit ):
Print ('terminate the procedure? ')
Flag = sys. stdin. readline (). rstrip ()
If (flag = 'yes') or (flag = 'y ')):
Sys. exit (1)
Else:
Continue
Except t:
Sys. exit (1)

In some cases, we require applications to have a one-way transmission function (not half-duplex, or duplex ), so we can use shutdown in Python to achieve our goal (whether there are other methods is unclear, please kindly advise)

The socket. shutdown (int) function is used to explain how to disable socket switch (int ):

Case 0: Disable future read

Case 1: Disable future writing

Case 2: Disable future read and write operations ("future" indicates that this function does not work for the requests currently processed)

This open socket is usually used in the following situations:

1. Ensure that all written data is sent

2. capture potential program errors

3. fork () or multithreading is used in the program, and some operations of other processes are prevented.

4 close socket now

 

 

From the column of FishinLab

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.