ConnectionError with Python 2.5 and SSL site

I am using requests 0.8.5 to GET the front page of an SSL site. This works fine with Python 2.7.2, Python 2.6.7, but throws ConnectionError with Python 2.5.6. Other SSL sites work as expected with Python 2.5.6.

Quick tests from the command-line:

```
veronica:test david$ python2.7 -c "import requests; print requests.get('https://viewpoint.toyota-europe.com/');"
<Response [200]>
veronica:test david$ python2.6 -c "import requests; print requests.get('https://viewpoint.toyota-europe.com/');"
<Response [200]>
veronica:test david$ python2.5 -c "import requests; print requests.get('https://viewpoint.toyota-europe.com/');"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "requests/api.py", line 80, in get
    return request('get', url, **kwargs)
  File "requests/api.py", line 67, in request
    prefetch=prefetch
  File "requests/sessions.py", line 191, in request
    r.send(prefetch=prefetch)
  File "requests/models.py", line 453, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: Max retries exceeded for url: /
veronica:test david$ 
```

But this works in Python 2.5:

```
veronica:test david$ python2.5 -c "import requests; print requests.get('https://github.com/');"
<Response [200]>
```

The problem web server is running IIS7. I will try and get more details soon. Platform is Mac OS X 10.7.2, all Python versions built from MacPorts.


Hmm, very odd. Oddly enough, it's working properly on my local copy of Python 2.5.6. 

Do let me know what you come up with.


Mistake, I am using requests version 0.8.3


The test works fine using a local proxy. Dammit. Turning up the log level I see

```
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (2): viewpoint.toyota-europe.com
WARNING:requests.packages.urllib3.connectionpool:Retrying (0 attempts remain) after connection broken by 'error(54, 'Connection reset by peer')': /
```

So the server really is forcing the retries error.


I think the problem is the server is closing the connection when there is no 'Host:' header. Will get an example working.


OK. Adding a Host header fixes the problem on Python 2.5 for me. So it is a server problem. Incidentally, Python's urllib2.urlopen() adds a Host header by default.

An example of a hook that adds the Host header if it is missing:

```
import requests
from urlparse import urlparse


def pre_request_hook(req):
    """Add a 'Host' header if it does not exist."""
    if 'Host' not in req.headers:
        hostname = urlparse(req.full_url)[1]
        req.headers['Host'] = hostname


hooks = {'pre_request': pre_request_hook}
url = 'https://viewpoint.toyota-europe.com/'
resp = requests.get(url, hooks=hooks)
print "With host header:", resp.status_code
```


There should definitely be a host header. Will investigate.


I went spelunking through httplib, and on Python 2.5.6 it definitely wasn't adding a host header for me automatically. It was only when I started going through urllib2 that I noticed it was always creating a host header.


httplib on my Python 2.5.6 _does_ add the host, but for https connections it adds the port in the header like `Host: viewpoint.toyota-europe.com:443`. This default host value is causing the server to close the connection, even though it is indeed listening on port 443.

```
import requests

# Request succeeds
requests.get('https://viewpoint.toyota-europe.com/', headers={'Host': 'viewpoint.toyota-europe.com'})

# Request raises ConnectionError
requests.get('https://viewpoint.toyota-europe.com/', headers={'Host': 'viewpoint.toyota-europe.com:443'})
```

See HTTPConnection.putrequest() in httplib 2.5.6 http://hg.python.org/cpython/file/de34c7b097e8/Lib/httplib.py#l816 where the test is for `HTTP_PORT`. In later versions, http://hg.python.org/cpython/file/8527427914a2/Lib/httplib.py#l899 , the test is for `self.default_port` which will be 443 for https connections (therefore the port number is omitted from the host header).


I'm having the same problem and found just to do a get request through my company's firewall/proxy that I had to set the 'Host" to the domain of the site I'm trying to connect to. However fails to work when I try to connect to an https site. 


Python 2.5 is no longer supported (in favor of Python 3). Sorry!