httplib design question

Gustaf Liljegren gustafl at algonet.se
Sat Jun 2 09:50:51 EDT 2001


Python 2.0 (Win32):
===================

I have noticed that the HTTP method doesn't try to connect to the server:

>>> h = httplib.HTTP('abc')
>>>

So I can give it a bad URL, and things will be fine until I try to get 
something with the putrequest() method.

>>> h.putrequest('GET', '/index.html')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "e:\python20\lib\httplib.py", line 425, in putrequest
    self.send(str)
  File "e:\python20\lib\httplib.py", line 367, in send
    self.connect()
  File "e:\python20\lib\httplib.py", line 351, in connect
    self.sock.connect((self.host, self.port))
  File "<string>", line 1, in connect
socket.error: host not found
>>>

Why doesn't the HTTP method handle this check instead? Here's what I'm 
trying to do:

try:
  h = httplib.HTTP(host)
except:
  print "Sorry. Can't find " + host
  sys.exit()

The workaround I found is to use the connect() method:

try:
  h = httplib.HTTP()
  h = h.connect(host)
except:
  print "Sorry. Can't find the host " + host
  sys.exit()

Regards,

Gustaf Liljegren



More information about the Python-list mailing list