【问题标题】:Make request with HTTPS proxy in python在 python 中使用 HTTPS 代理发出请求
【发布时间】:2021-02-17 17:38:10
【问题描述】:

我的 VPN 提供商 (NordVP) 刚刚关闭了其代理服务器上的所有 HTTP 端口。我可以通过cURL 确认 HTTPS 端口工作正常,例如以下工作:

curl -x https://proxy_server:89 --proxy-user username:password -L url

虽然这刚刚停止工作:

curl -x http://proxy_server:80 --proxy-user username:password -L url

两天前它曾经工作过。在 python 中,我尝试使用 request 模块发出 HTTP 请求:

import requests
proxies = {
  "https":"https://user:password@proxy_server:port"
}

response = requests.get("https://api64.ipify.org?format=json", proxies=proxies)
print(response.text)

但我收到以下 3 个错误:

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

...

MaxRetryError: HTTPSConnectionPool(host='api64.ipify.org', port=443): Max retries exceeded with url: /?format=json (Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)))

During handling of the above exception, another exception occurred:

...

ProxyError: HTTPSConnectionPool(host='api64.ipify.org', port=443): Max retries exceeded with url: /?format=json (Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)))

我看到有一个关于该主题的非常古老的问题,但它似乎不再相关。

【问题讨论】:

    标签: python https proxy


    【解决方案1】:

    这里有一个解决方法:

    Authenticator proxyAuthenticator = new Authenticator() {
        @Override public Request authenticate(Route route, Response response) throws IOException {
            String credential = Credentials.basic(username, password);
            return response.request().newBuilder().header("Proxy-Authorization", credential).build();
        }
    };
    
    OkHttpClient client = new OkHttpClient.Builder()
            .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
            .proxyAuthenticator(proxyAuthenticator);
            .socketFactory(new DelegatingSocketFactory(SSLSocketFactory.getDefault()))
            .build();
    

    在 Android 上将此添加到您的 gradle:

    implementation 'org.conscrypt:conscrypt-android:2.5.0'
    

    这个给你的应用onCreate():

    Security.insertProviderAt(Conscrypt.newProvider(), 1);
    

    https://github.com/square/okhttp/issues/6561

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      相关资源
      最近更新 更多