Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 20th, 2005, 05:15 AM
Sells, Fred
Guest
 
Posts: n/a
Default UDP socket, need help setting sending port

I'm using MSW XP Pro with Python 2.4 to develop but production will be Linux
with Python 2.3. (could upgrade to 2.4 if absolutely necessary) I can also
switch to Linux for development if necessary.

I am writing some python to replace proprietary software that talks to a
timeclock via UDP.

The timeclock extracts the sending port from the UDP header and uses that
for all response messages.

I cannot find out how to set the sending port in the header. Windows XP
appears to set an arbitrary port. I've been using ethereal to analyze
network traffic and it seems that if I can set the sending port, I should be
OK.

I have been googling various combinations of "python udp ..." for the last
two hours and have not found anything that addresses how to set the sending
port. I'm guessing that this may be in setsockopt but don't see any
parameters that "click".

Any help would be greatly appreciated.

Fred Sells
fred at adventistcare dotttttt org


---------------------------------------------------------------------------
The information contained in this message may be privileged and / or
confidential and protected from disclosure. If the reader of this message is
not the intended recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited. If you
have received this communication in error, please notify the sender
immediately by replying to this message and deleting the material from any
computer.
---------------------------------------------------------------------------
  #2  
Old December 20th, 2005, 09:45 AM
keirr
Guest
 
Posts: n/a
Default Re: UDP socket, need help setting sending port

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.

  #3  
Old December 20th, 2005, 09:55 AM
keirr
Guest
 
Posts: n/a
Default Re: UDP socket, need help setting sending port

A few trivial corrections, to my own post :-(
tc_sock = socket(socket...
should be
tc_sock = socket.socket(socket...
of course

and, (while I'm here) when I stated that calling connect on an
unbound socket caused
a ephemeral port to be assigned, I should have written "calling connect
on an unbound _UDP_ socket, etc. "

Cheers,

Keir.

  #4  
Old December 22nd, 2005, 08:45 PM
Steve Horsley
Guest
 
Posts: n/a
Default Re: UDP socket, need help setting sending port

Sells, Fred wrote:[color=blue]
> I'm using MSW XP Pro with Python 2.4 to develop but production will be Linux
> with Python 2.3. (could upgrade to 2.4 if absolutely necessary) I can also
> switch to Linux for development if necessary.
>
> I am writing some python to replace proprietary software that talks to a
> timeclock via UDP.
>
> The timeclock extracts the sending port from the UDP header and uses that
> for all response messages.
>
> I cannot find out how to set the sending port in the header. Windows XP
> appears to set an arbitrary port. I've been using ethereal to analyze
> network traffic and it seems that if I can set the sending port, I should be
> OK.
>
> I have been googling various combinations of "python udp ..." for the last
> two hours and have not found anything that addresses how to set the sending
> port. I'm guessing that this may be in setsockopt but don't see any
> parameters that "click".[/color]


Try binding the address ('',0) which means any address, any port
on this box. Then get the bound address with getsockname(). Below
is a copy of an interactive try-out...


[color=blue][color=green][color=darkred]
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> s.bind(('',0))
>>> s[/color][/color][/color]
<socket._socketobject object at 0xb7d9ee0c>[color=blue][color=green][color=darkred]
>>> s.getsockname()[/color][/color][/color]
('0.0.0.0', 32775)[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

So in this case, port 32775 was chosen to bind to.

Steve
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles