Re: how to get any available port

2005-10-05 Thread Steve Holden
[EMAIL PROTECTED] wrote:
 Apparently, calling bind() with a zero port will choose some available port
 number, as demonstrated by this program:
 
 import socket
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.bind((, 0))
 print s.getsockname()
 
 Here's how it behaved over several runs:
 $ python soc.py 
 ('0.0.0.0', 34205)
 $ python soc.py 
 ('0.0.0.0', 34206)
 $ python soc.py 
 ('0.0.0.0', 34207)
 
 I don't know for sure whether this is standard behavior for sockets, or 
 whether
 it's a special behavior of linux.
 
It's been standard behaviour ever since the Berkeley socket interface 
was defined, as far as I know.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

-- 
http://mail.python.org/mailman/listinfo/python-list


how to get any available port

2005-10-04 Thread Mohammed Smadi
hi;
if am using s.bind for a tcp socket.  On the client side i dont really 
care which socket i use as long as i get an available socket.  Is there a 
funciton or a way to get an available socket?

thanks
smadi

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Fredrik Lundh
Mohammed Smadi wrote:

 if am using s.bind for a tcp socket.  On the client side i dont really
 care which socket i use as long as i get an available socket.  Is there a
 funciton or a way to get an available socket?

why are you using bind if you're on the client side?

/F 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread ncf
Hmm...perhaps he is trying to do a transfer thing like many chat
programs do. Instead of sending large files across a server, you
Direct Connect and send the file directly. :shrugs:

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Grant Edwards
On 2005-10-04, ncf [EMAIL PROTECTED] wrote:

 Hmm...perhaps he is trying to do a transfer thing like many chat
 programs do. Instead of sending large files across a server, you
 Direct Connect and send the file directly. :shrugs:

So how does that require binding the client end of a TCP
connection?

-- 
Grant Edwards   grante Yow!  I want you to
  at   MEMORIZE the collected
   visi.compoems of EDNA ST VINCENT
   MILLAY... BACKWARDS!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Paul Rubin
Grant Edwards [EMAIL PROTECTED] writes:
  Hmm...perhaps he is trying to do a transfer thing like many chat
  programs do. Instead of sending large files across a server, you
  Direct Connect and send the file directly. :shrugs:
 
 So how does that require binding the client end of a TCP connection?

In the nomenclature of some of these applications, that kind of transfer
is called a client to client connection.  Both ends are called clients.
-- 
http://mail.python.org/mailman/listinfo/python-list



Re: how to get any available port

2005-10-04 Thread Grant Edwards
On 2005-10-04, Paul Rubin  wrote:
 Grant Edwards [EMAIL PROTECTED] writes:
  Hmm...perhaps he is trying to do a transfer thing like many chat
  programs do. Instead of sending large files across a server, you
  Direct Connect and send the file directly. :shrugs:
 
 So how does that require binding the client end of a TCP connection?

 In the nomenclature of some of these applications, that kind
 of transfer is called a client to client connection.  Both
 ends are called clients.

IIRC, we were talking about TCP sockets.  In a TCP connection,
one end is the client (the one that originates the connection),
and the other end is the server (the one that waits for the
connection on a well-known port).

The server calls bind()/listen(), and typically the client just
calls connect().  In theory, the client end can call bind() to
control the originating port used for the connection. But,
servers don't care what the orignating port is, so on the
client end bind() is usually skipped since the default
connect() operation will just choose an available port.

-- 
Grant Edwards   grante Yow!  It's strange, but I'm
  at   only TRULY ALIVE when I'm
   visi.comcovered in POLKA DOTS and
   TACO SAUCE...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread jepler
Apparently, calling bind() with a zero port will choose some available port
number, as demonstrated by this program:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((, 0))
print s.getsockname()

Here's how it behaved over several runs:
$ python soc.py 
('0.0.0.0', 34205)
$ python soc.py 
('0.0.0.0', 34206)
$ python soc.py 
('0.0.0.0', 34207)

I don't know for sure whether this is standard behavior for sockets, or whether
it's a special behavior of linux.

Jeff


pgpdXrF07MIY9.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to get any available port

2005-10-04 Thread Mohammed Smadi
On Tue, 4 Oct 2005, Grant Edwards wrote:

 On 2005-10-04, ncf [EMAIL PROTECTED] wrote:
 
  Hmm...perhaps he is trying to do a transfer thing like many chat
  programs do. Instead of sending large files across a server, you
  Direct Connect and send the file directly. :shrugs:
 
 So how does that require binding the client end of a TCP
 connection?
 
 
what else would you do?  I am using examples from the web and they all 
bind to a port at the localhost before connecting to the remote host.

my code is like this

#transmission socket
s = socket.socket(socket.AF_INET,  socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((,hp_port)) # do some error checking


data=HI
print data
s.connect(('192.168.2.13',port))
s.send(data)
print \n

which is working fine for me after putting the 
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) statement

any suggestions for alternative implementation?

moe smadi

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Paul Rubin
Grant Edwards [EMAIL PROTECTED] writes:
  In the nomenclature of some of these applications, that kind
  of transfer is called a client to client connection.  Both
  ends are called clients.
 
 IIRC, we were talking about TCP sockets.

Yes, but if the person was talking about using TCPs sockets in a chat
application, they may have been using chat terminology rather than TCP
terminology.  In a typical chat app, a zillion clients all talk
through TCP to a central server, which is like a phone switch.  That
lets Alice connect to the server and immediately see if Bob is online
and what Bob's (possibly dynamic) IP address is.  If Alice wants to
send Bob a file, she opens a TCP connection directly to Bob.  That's
called a client to client connection because Alice and Bob are both
clients.

So, there's a reasonable application for wanting to open a listener
port without binding any specific port number.  Alice would just ask
her OS to assign her a port (say it assigns 23789) and listen on it,
instead of having to contend with other apps on the same IP for some
specific port number.  Then she'd send a message through the chat
server asking Bob to connect to port 23789 on her machine.

I'm not sure if there's a way to do this.  Do you happen to know?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread jepler
On Tue, Oct 04, 2005 at 05:19:37PM -0400, Mohammed Smadi wrote:
 what else would you do?  I am using examples from the web and they all 
 bind to a port at the localhost before connecting to the remote host.
[...]

the web must be stupider than I thought.

Here's how Python's own ftplib connects to an ftp server (dedented for clarity):
try:
self.sock = socket.socket(af, socktype, proto)
self.sock.connect(sa)
except socket.error, msg:
...

there's no need to call bind() before connect.

Jeff


pgpHhphJklBxU.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to get any available port

2005-10-04 Thread Paul Rubin
Mohammed Smadi [EMAIL PROTECTED] writes:
 #transmission socket
 s = socket.socket(socket.AF_INET,  socket.SOCK_STREAM)
 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 s.bind((,hp_port)) # do some error checking
 ...
 any suggestions for alternative implementation?

In this particular instance, don't bind the transmission socket.  I'm
also not sure why you set SO_REUSEADDR.  I've done some socket
programming but am no expert.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Fredrik Lundh
Mohammed Smadi wrote:

 what else would you do?  I am using examples from the web and they all
 bind to a port at the localhost before connecting to the remote host.

pointers, please.

 my code is like this

 #transmission socket
 s = socket.socket(socket.AF_INET,  socket.SOCK_STREAM)
 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 s.bind((,hp_port)) # do some error checking

that's a typical server setup.

 data=HI
 print data
 s.connect(('192.168.2.13',port))
 s.send(data)

and this is typical client setup.

are you sure you're not cutting and pasting code from different examples?

 any suggestions for alternative implementation?

if you're writing a client, get rid of the setsockopt and bind stuff.

/F 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Grant Edwards
On 2005-10-04, Mohammed Smadi [EMAIL PROTECTED] wrote:
 On Tue, 4 Oct 2005, Grant Edwards wrote:

 On 2005-10-04, ncf [EMAIL PROTECTED] wrote:
 
  Hmm...perhaps he is trying to do a transfer thing like many chat
  programs do. Instead of sending large files across a server, you
  Direct Connect and send the file directly. :shrugs:
 
 So how does that require binding the client end of a TCP
 connection?

 what else would you do?

Just call connect():

 I am using examples from the web and they all bind to a port
 at the localhost before connecting to the remote host.

I don't know who wrote those examles, but I've never seen it
done that way before.  Take a look at the examples from the
python socket module docs:

http://docs.python.org/lib/socket-example.html

 my code is like this

 #transmission socket
 s = socket.socket(socket.AF_INET,  socket.SOCK_STREAM)
 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 s.bind((,hp_port)) # do some error checking


 data=HI
 print data
 s.connect(('192.168.2.13',port))
 s.send(data)
 print \n

 which is working fine for me after putting the 
 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) statement

Just skip the bind() call.  It's useless.

#transmission socket

s = socket.socket(socket.AF_INET,  socket.SOCK_STREAM)
s.connect(('192.168.2.13',port))
s.send(data)

-- 
Grant Edwards   grante Yow!  Are you still an
  at   ALCOHOLIC?
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Grant Edwards
On 2005-10-04, Paul Rubin  wrote:
 Mohammed Smadi [EMAIL PROTECTED] writes:
 #transmission socket
 s = socket.socket(socket.AF_INET,  socket.SOCK_STREAM)
 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 s.bind((,hp_port)) # do some error checking
 ...
 any suggestions for alternative implementation?

 In this particular instance, don't bind the transmission socket.  I'm
 also not sure why you set SO_REUSEADDR.  I've done some socket
 programming but am no expert.

You set SO_REUSEADDR when you need to bind to a specific port,
and you want to be able to re-use that port address for a new
connection without waiting for the time specified in the TCP
RFC after the old connection was closed.

In the OP's case, there is no need to bind to a specific port,
so just getting rid of the bind() and setsockopt() calls is the
obvious answer.

-- 
Grant Edwards   grante Yow!  If I had a Q-TIP, I
  at   could prevent th' collapse
   visi.comof NEGOTIATIONS!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Grant Edwards
On 2005-10-04, Paul Rubin  wrote:
 Grant Edwards [EMAIL PROTECTED] writes:
  In the nomenclature of some of these applications, that kind
  of transfer is called a client to client connection.  Both
  ends are called clients.
 
 IIRC, we were talking about TCP sockets.

 So, there's a reasonable application for wanting to open a
 listener port without binding any specific port number.  Alice
 would just ask her OS to assign her a port (say it assigns
 23789) and listen on it, instead of having to contend with
 other apps on the same IP for some specific port number.  Then
 she'd send a message through the chat server asking Bob to
 connect to port 23789 on her machine.

Ah.  Got it.

 I'm not sure if there's a way to do this.  Do you happen to
 know?

IIRC, you just call bind() with a port number of zero, and then
use some method-or-other on the bound socket to find out what
port it's bound to.

-- 
Grant Edwards   grante Yow!  Life is a POPULARITY
  at   CONTEST! I'm REFRESHINGLY
   visi.comCANDID!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Fredrik Lundh
Grant Edwards wrote:

 IIRC, you just call bind() with a port number of zero, and then
 use some method-or-other on the bound socket to find out what
 port it's bound to.

 s = socket.socket()
 s.bind((, 0))
 s.getsockaddr()
(0.0.0.0, 4711)

/F 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Paul Rubin
Fredrik Lundh [EMAIL PROTECTED] writes:
 Grant Edwards wrote:
  IIRC, you just call bind() with a port number of zero, and then
  use some method-or-other on the bound socket to find out what
  port it's bound to.
 
  s = socket.socket()
  s.bind((, 0))
  s.getsockaddr()
 (0.0.0.0, 4711)

Nice.  Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get any available port

2005-10-04 Thread Cameron Laird
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] wrote:
.
.
.
Here's how it behaved over several runs:
$ python soc.py 
('0.0.0.0', 34205)
$ python soc.py 
('0.0.0.0', 34206)
$ python soc.py 
('0.0.0.0', 34207)

I don't know for sure whether this is standard behavior for sockets, or whether
it's a special behavior of linux.
.
.
.
It is standard behavior for sockets.  To my surprise, I haven't
(yet) found an RFC that specifies it, but the behavior has a deep
and wide history--even Microsoft sockets respect the convention.
-- 
http://mail.python.org/mailman/listinfo/python-list