https获取请求python(https/SSL)



所以我正在将我的一个java程序转换为python。在我的java代码中,我有一个http-get调用,它看起来像这样。

sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} }, new SecureRandom());
try {
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(new org.apache.http.conn.ssl.SSLSocketFactory(sslContext)).build();
String authString = username + ":" + password;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
HttpGet httpGet = new HttpGet(envURL);
httpGet.setHeader("Content-Type", "application/json");
httpGet.setHeader("Authorization", "Basic " + authStringEnc);
CloseableHttpResponse httpGetResponse = httpclient.execute(httpGet);
HttpEntity entityResponse = httpGetResponse.getEntity();
String result = EntityUtils.toString(entityResponse);
EntityUtils.consume(entityResponse);
JSONParser parser = new JSONParser();
thresholdContent = (JSONArray) parser.parse(result);
} catch (Exception e) {
e.printStackTrace();
}

我正试图在python 3.x中找到最干净的方法来做到这一点。或者我想在python中做这样的事情是最合适的。

我试过这样的东西:

conn = requests.get(env, headers={"content-type":"application/json"}, auth=(userName,password))

但运气不太好。

对于python中的请求,您需要传递url

conn = requests.get(url = 'https://myurl', headers = {'Content-Type':'application/json'})

最新更新