我認爲我自己是Python和Requests/Urllib2庫的高級開發人員。Python請求或Urllib2代理設置在OSX上被忽略Capitan 10.11.6
我最近切換到使用Parallels運行OSX和Win10的Mac。 當使用代理選擇方法運行上述庫中的任何一個時,OSX忽略自定義http請求代理設置並繼續使用我的公有IP,如果我使用Win10,則不會發生這種情況。
方法嘗試:
def simple_request(self):
base_url = "www.whatever.com"
proxy = "http://192.168.2.1:8080"
# i am using jsonip.com as a host to get my request IP
r = requests.get(r'http://jsonip.com', proxies={"http": proxy})
ip = r.json()['ip'] # will always be my public ip
我已經使用了兩種方式測試有效代理:
使用採用相同的方法Win10 Python腳本。
將相同的代理設置應用於OSX網絡首選項管理器。
而且,我絕不會出現以下錯誤,使用無效的代理即便:
requests.exceptions.ConnectionError: HTTPConnectionPool Max retries exceeded with url
我做當我申請無效的代理設置OSX的網絡偏好設置管理器出現此錯誤。
我已經嘗試在osx下使用代理服務器提到這兩個庫,我得到了同樣的結果。
其他解決方案的嘗試:
使用Requests.Session經理髮送之前構建與代理的請求。
使用以下https://stackoverflow.com/a/39934154/4255373 - 它
會改變我的設置,但我將不得不重新啓動我的代碼中使用它, 和改變連接設置整個操作系統服務不 對我來說是有效的解決方案。
有什麼建議嗎?
謝謝。
更新: 使用日誌模塊我得到如下:
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 199.116.113.206
send: 'GET http://jsonip.com/ HTTP/1.1\r\nHost: jsonip.com\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.11.1\r\n\r\n'
reply: 'HTTP/1.1 301 Moved Permanently\r\n'
header: Date: Thu, 12 Jan 2017 02:13:25 GMT
header: Server: nginx/1.10.0 (Ubuntu)
header: Content-Type: text/html
header: Content-Length: 194
header: Location: https://jsonip.com/
header: Keep-Alive: timeout=5, max=100
header: Connection: Keep-Alive
DEBUG:requests.packages.urllib3.connectionpool:"GET http://jsonip.com/ HTTP/1.1" 301 194
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): jsonip.com
send: 'GET/HTTP/1.1\r\nHost: jsonip.com\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.11.1\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Server: nginx/1.10.0 (Ubuntu)
header: Date: Thu, 12 Jan 2017 02:13:25 GMT
header: Content-Type: application/json; charset=utf-8
header: Transfer-Encoding: chunked
header: Connection: keep-alive
header: Access-Control-Allow-Origin: *
header: Access-Control-Allow-Methods: GET
header: Strict-Transport-Security: max-age=31536000;
DEBUG:requests.packages.urllib3.connectionpool:"GET/HTTP/1.1" 200 None
你應該展示更多的回溯,這可能有助於縮小範圍。 –
回溯?我沒有例外。 Https服務器和http代理錯誤 –
它可以創造奇蹟... :) –