0

I am trying to import a python library using:

import cenpy as cp

but I get an error message:

ConnectionError: HTTPSConnectionPool(host='api.census.gov', port=443): Max retries exceeded with url: /data.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000013167B552B0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))

I have had this issue before while calling a website. It has to do with the proxy settings. I resolved those other issues using code like this:

import requests

s = requests.Session()

s.proxies = {
     "https":"https://user:pass@server:port", 
     "http":"http://user:pass@server:port"
     }

and then:

s.get('http://web.address')

Is there anyway to implement the request session so that I am able to import the library?

Using Python 3.9.12

conduit
  • 28
  • 5

1 Answers1

0

So I did some more digging and found out the library does place a call to the API during import. There seems to be a workaround for this but it is not implemented their code yet. I tried a few more things and I wanted to share what worked for me. You have to make sure that the code below runs prior to importing the library making the call. This code should allow for all other call/get requests to run through the proxy without having to use a requests session.

The snippets below will set the proxy environment variables

import os
os.environ['http_proxy'] = 'http://<user>:<pass>@<proxy>:<port>'
os.environ['https_proxy'] = 'http://<user>:<pass>@<proxy>:<port>'

Or to be more thorough:

import os
proxy = 'http://<user>:<pass>@<proxy>:<port>'
os.environ['http_proxy'] = proxy 
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy

Remember that this should be at the very top of your script, or at least prior to any connection requests. Also, make sure you are using the correct IP address for the proxy, as that tripped me up as well.

Credit goes here and here.

conduit
  • 28
  • 5