0

I'm trying to download files from a website that uses HttpBasicAuth. Everything worked fine on an Ubuntu machine but not when I tried to run my script on a Windows machine. I have problems downloading files from that website using requests. Below is the code relevant to my problem:

Initial code that works on Ubuntu:

url = 'https://abc.example.com'
s = requests.Session()
s.verify = False
s.auth = (creds['user'], creds['passwd'])
filename = "somefile.txt"
download_url = url + "/" + filename
resp = s.get(download_url)  # fails on this line when run in Windows

I did a verify=False because I'm having SSL3_GET_SERVER_CERTIFICATE:certificate verify failed. if I don't. I don't understand fully what that means and I would appreciate an explanation. From how I understood it, requests just cannot verify the server's cert. Similarly, it seemed that my browser does not recognize the certificate either as seen below.

browser cannot recognize

Anyways, so I ran the script on Windows that has pretty much the same python version (2.7.x) and I got this:

exception shown

I did a quick search and found some fixes here and found a github issue here. I followed the first fix and changed my code to:

url = 'https://abc.example.com'
s = requests.Session()
s.verify = False
s.auth = (creds['user'], creds['passwd'])
s.mount('https://', MyAdapter())
# .. omitted code similar to the one above ..
resp = s.get(download_url)

I added the class specified in the answer

class MyAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_TLSv1)

It is still showing the same errors.

From the github issue, they were checking the OpenSSL version, so I did a check on the Windows machine and if I remember correctly it is 1.0.2a. I did a check on the Ubuntu machine where the initial version was working, it is 1.0.1f.

EDIT:

Python version in Ubuntu: 2.7.6 (works here)

Python version in Windows: 2.7.10 (does not work here)

Community
  • 1
  • 1
krato
  • 1,226
  • 4
  • 14
  • 30
  • Have you checked that your root certificates are up to date on your Windows machine? – mhawke Apr 04 '16 at 11:27
  • Do not add text as images, copy/paste it instead. – ivan_pozdeev Apr 04 '16 at 11:27
  • Sniffing the traffic may tell what exactly is wrong. If there's a protocol violation, the error probably corresponds to packet structure which is not encrypted. – ivan_pozdeev Apr 04 '16 at 11:29
  • Since the stack trace shows "adapter.py", the problem can be in the Adapter i.e. in the code differences rather that OS differences. – ivan_pozdeev Apr 04 '16 at 11:34
  • @ivan_pozdeev I found it weird as well because doesn't python have the same code for the modules across different OSes? If that i so, then the initial code without using MyAdapter should have worked. – krato Apr 04 '16 at 11:39
  • In YOUR code differences. The Linux code doesn't use any `Adapter`s. From what I can see, you run _different_ code on Linux and Wnidows. If it's not so, please edit your question to clearly restate your steps: what you've run, where, and with what results. It's unclear now, and we're no psychics. – ivan_pozdeev Apr 04 '16 at 12:36
  • I don't see why it's unclear. I ran the first snippet for both machines but since it did not work in Windows, I have to make changes which still produced the same error. – krato Apr 04 '16 at 12:56
  • "... pretty much the same python version (2.7.x) " - please make sure that you are using at least version 2.7.9, which is the first 2.7.x version supporting [SNI](https://en.wikipedia.org/wiki/Server_Name_Indication). Having SNI support on one platform but not on the other might explain the different behavior. – Steffen Ullrich Apr 04 '16 at 19:06
  • @SteffenUllrich I updated my question to show the exact versions. It is odd that it works on the earlier version of python – krato Apr 05 '16 at 04:11
  • @krato: can you publish the URL of this site so one is able to reproduce the problem? My guess is that it is somehow related to the setup of this site. – Steffen Ullrich Apr 05 '16 at 04:22
  • @SteffenUllrich, sorry but the URL is from the company I'm developing for and is confidential. I also guess that the site's setup can be causing problems knowing that chrome itself cannot verify the certificate but I still think there may be some way to go around this since it worked on an Ubuntu system with a different python version. – krato Apr 05 '16 at 06:29
  • @krato: I'm sure that it can be worked around. But since the exact cause of the problem is not known so far it is not possible to say **how** it can be worked around. – Steffen Ullrich Apr 05 '16 at 07:05

0 Answers0