@@ -24,35 +24,37 @@ def eprint(*args, **kwargs):
2424    SOL_UDP  =  17   # Protocol number for UDP 
2525    UDP_SEGMENT  =  103   # Option code for UDP segmentation (non-standard) 
2626
27- # Get the IP and port from command-line arguments 
28- if  len (sys .argv ) !=  3 :
29-     eprint ("Usage: python3 udp_offload.py <ip_address> <port>" )
30-     sys .exit (1 )
3127
32- ip_address  =  sys .argv [1 ]
33- port  =  int (sys .argv [2 ])
34- 
35- # Create a UDP socket 
36- sockfd  =  socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
37- 
38- # Set the UDP segmentation option (UDP_SEGMENT) to 1400 bytes 
39- OPTVAL  =  1400 
40- try :
41-     sockfd .setsockopt (SOL_UDP , UDP_SEGMENT , OPTVAL )
42- except  (AttributeError , PermissionError ):
43-     eprint ("Unable to set UDP_SEGMENT option" )
44-     sys .exit (1 )
45- 
46- # Set the destination address and port 
47- servaddr  =  (ip_address , port )
48- 
49- # Send the message to the destination address 
50- MESSAGE  =  b"x" 
51- try :
52-     sockfd .sendto (MESSAGE , servaddr )
53-     print ("Message sent successfully" )
54- except  socket .error  as  e :
55-     eprint (f"Error sending message: { e }  " )
56-     sys .exit (1 )
57- 
58- sockfd .close ()
28+ if  __name__  ==  "__main__" :
29+     # Get the IP and port from command-line arguments 
30+     if  len (sys .argv ) !=  3 :
31+         eprint ("Usage: python3 udp_offload.py <ip_address> <port>" )
32+         sys .exit (1 )
33+ 
34+     ip_address  =  sys .argv [1 ]
35+     port  =  int (sys .argv [2 ])
36+ 
37+     # Create a UDP socket 
38+     sockfd  =  socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
39+ 
40+     # Set the UDP segmentation option (UDP_SEGMENT) to 1400 bytes 
41+     OPTVAL  =  1400 
42+     try :
43+         sockfd .setsockopt (SOL_UDP , UDP_SEGMENT , OPTVAL )
44+     except  (AttributeError , PermissionError ):
45+         eprint ("Unable to set UDP_SEGMENT option" )
46+         sys .exit (1 )
47+ 
48+     # Set the destination address and port 
49+     servaddr  =  (ip_address , port )
50+ 
51+     # Send the message to the destination address 
52+     MESSAGE  =  b"x" 
53+     try :
54+         sockfd .sendto (MESSAGE , servaddr )
55+         print ("Message sent successfully" )
56+     except  socket .error  as  e :
57+         eprint (f"Error sending message: { e }  " )
58+         sys .exit (1 )
59+ 
60+     sockfd .close ()
0 commit comments