如何在Python脚本中设置代理

时间:2017-10-03 21:09:35

标签: python python-requests

我尝试过使用requests.post

def creat(script,reason):

    split=script.split('.')
    json_file=LOG_DIR+split[0]+".json"

    url='my url'
    p1={'proxy1','proxy2'}
    createJSON(script,json_file,reason)
    fp=open(json_file,'rb').read()
    print "\nJSON File :"
    print fp
    b=requests.post(url,proxies=p1,data=fp,headers=headers,auth=auth)
    # s = requests.session()
    #s.proxies.update(p1)
    #s.post(p1)
    content=b.json()

脚本执行但看起来似乎没有应用代理。错误是

requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

1 个答案:

答案 0 :(得分:0)

proxies参数需要dict,而不是set

dict应该是协议到主机,或者协议和主机到主机的映射,如documentation

中所指定的
  

proxies = None

     

字典映射协议或协议和主机到每个{'http': 'foo.bar:3128', 'http://host.name': 'foo.bar:4012'}上使用的代理的URL(例如> Request)。

如果您尝试为HTTPS连接指定代理,则它应如下所示:

import requests

# ... your stuff ...

# Without knowing what either of your proxies are meant for,
# I'm going to just specify one proxy here for ALL https connections
proxies = { 'https': 'proxy1' }

# ... more of your stuff ...
b = requests.post(url, proxies=proxies, data=fp, headers=headers, auth=auth)

# ... more and more of your stuff ...