Binding socket to an address

Peter Hansen peter at engcorp.com
Tue Jun 10 17:29:13 CEST 2003


"Morten W. Petersen" wrote:
> 
> I'm trying to bind httplib.HTTP requests to a local IP, using this class:
> 
> class custom_http(httplib.HTTP):
> 
>     def connect(self):
>         """Connect to the host and port specified in __init__."""
>         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>         self.sock.bind(('216.93.187.217', 1234))
>         self.sock.connect((self.host, self.port))
> 
> However, it doesn't work.  What am I doing wrong?

Works here.  Please define "doesn't work", preferably with a traceback
or specific symptoms.  In my case, I just did this from a DOS prompt:

In window #1:

>>> from socket import *
>>> s = socket(AF_INET, SOCK_STREAM)
>>> s.bind(('', 5555))
>>> s.listen(5)
>>> c = s.accept()
# there's a pause here while I connect from window #2, below
>>> c
(<socket._socketobject instance at 007A560C>, ('192.168.0.66', 6666))

In window #2:

>>> from socket import *
>>> s = socket(AF_INET, SOCK_STREAM)
>>> s.bind(('192.168.0.66', 6666))
>>> s.connect(('localhost', 5555))
>>> s.getpeername()
('127.0.0.1', 5555)

-Peter




More information about the Python-list mailing list