Python Proxy Settings

2019-05-04 00:58发布

问题:

I was using the wikipedia module in which you can get the information that is present about that topic on wikipedia. When I run the code it is unable to connect because of proxy. When I connected PC to a proxy free network it's working. It also happened while using the Beautiful soup module for scraping. I have tried to set environment variable like http://username:password@proxy_url:port but when the run the code in IDLE it's not working. Please help.

回答1:

It worked:

import os
os.environ["HTTPS_PROXY"] = "http://user_id:pass@proxy:port"


回答2:

If you don't want to store your password in code file:

import os

pxuser = "your.corporate.domain\\your_username"
pxpass = input(f"Password for {pxuser}: ")
env_px = f"http://{pxuser}:{pxpass}@your_proxy:port"
os.environ["HTTPS_PROXY"] = env_px


标签: python proxy