简体   繁体   English

https获取请求python(HTTPS / SSL)

[英]https get request python (HTTPS / SSL)

So i'm working on converting a java program I have over to python. 所以我正在将我已经完成的Java程序转换为python。 In my java code I have a http get call that looks like this. 在我的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();
        }

i'm trying to find the cleanist way to do this in python 3.x. 我正在尝试在python 3.x中找到执行此操作的清洁方法。 Or I guess the standered for doing something like this in python. 或者我猜想在python中做这样的事情的人是有经验的。

I've tried soemthing like: 我尝试过类似的东西:

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

but have not had much luck. 但运气还不太好。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM