[Pythonmac-SIG] socket code works on Win32 but not Mac :-(

Ned Deily nad at acm.org
Thu Jan 15 19:02:37 CET 2009


In article <496F0FAC.30804 at cemery.org.uk>,
 Ranec <python-mac at cemery.org.uk> wrote:
> The following code works just fine under Windows XP and Vista but not on
> MacOS X (currently no idea what version, though Python is 2.5.?). Any
> thoughts?
> 
> def get_my_ip_address():
>     ret=None
>     try:
>         s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>         s.connect(('google.com', 0))
>         sn=s.getsockname()
>         if sn:
>             ret=sn[0]
>     except socket.error, e:
>         hn=socket.gethostname()
>         ret=socket.gethostbyname(hn)
>     return ret
> 
> produces the stack:
> 
> Traceback (most recent call last):
>   File "./sarnie_client.py", line 181, in get_my_ip_address
>     s.connect(('google.com', 0))
>   File "<string>", line 1, in connect
> socket.error: (49, "Can't assign requested address")
> 
> My application has been given firewall rights accept connections from
> the Internet. Is there perhaps anything else that I should configure?

Don't use port 0.

>>> import socket
>>> s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> s.connect(('google.com', 0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in connect
socket.error: [Errno 49] Can't assign requested address
>>> s.connect(('google.com', 1))
>>> sn = s.getsockname()
>>> sn
('10.20.30.40', 56621)

-- 
 Ned Deily,
 nad at acm.org



More information about the Pythonmac-SIG mailing list