2017-01-12 34 views
1

我認爲我自己是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 

我已經使用了兩種方式測試有效代理:

  1. 使用採用相同的方法Win10 Python腳本。

  2. 將相同的代理設置應用於OSX網絡首選項管理器。

而且,我絕不會出現以下錯誤,使用無效的代理即便:

requests.exceptions.ConnectionError: HTTPConnectionPool Max retries exceeded with url 

我做當我申請無效的代理設置OSX的網絡偏好設置管理器出現此錯誤。

我已經嘗試在osx下使用代理服務器提到這兩個庫,我得到了同樣的結果。

其他解決方案的嘗試:

  1. 使用Requests.Session經理髮送之前構建與代理的請求。

  2. 使用以下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 
+0

你應該展示更多的回溯,這可能有助於縮小範圍。 –

+0

回溯?我沒有例外。 Https服務器和http代理錯誤 –

+0

它可以創造奇蹟... :) –

回答

1

OK了新手的錯誤。

我按照歐萊雅L'升建議 - 簡單而重要的 - 並記錄到,我意識到這 我試圖使用HTTP代理連接到HTTPS服務器,因此它沒有控制檯中的所有urllib3連接後。

我還是不明白爲什麼代理服務器會自動忽略而沒有例外情況或警告信息打印到控制檯,爲什麼連接管理器會在沒有代理服務器的情況下繼續發送請求。