UDP socket, need help setting sending port

keirr keir.robinson at gmail.com
Tue Dec 20 10:38:00 CET 2005


Fred,

   It is quite possible I've misunderstood the problem :-)  but have
you tried anything like

   import socket
   tc_local_port =  9999
   tc_remote_port = 9999
   outgoing_if = "172.16.1.2"  # say
   remote_tc_host = "172.16.1.3" # say
   # udp is the default for DGRAM
   tc_sock = socket(socket.AF_INET, socket.SOCK_DGRAM)
   tc_sock.bind((outgoing_if, tc_local_port))
   tc_sock.connect((remote_tc_host, tc_remote_port))

   If you send data with tc_sock, it should have a source port of
tc_local_port.
   So, to set the source port, call bind.  You should do that before
calling connect, since
   calling connect on an unbound socket has the side effect of
assigning an ephemeral port to the socket.
   Hope that was of some help to you.

All the best,

 Keir.




More information about the Python-list mailing list