HTTPS连接Python [英] HTTPS connection Python

查看:169
本文介绍了HTTPS连接Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证该目标是否公开了https网络服务。我有通过HTTP连接的代码,但我不知道如何通过HTTPS连接。我已经读过您使用SSL但我也读过它不支持证书错误。我得到的代码来自python docs:

I am trying to verify the that target exposes a https web service. I have code to connect via HTTP but I am not sure how to connect via HTTPS. I have read you use SSL but I have also read that it did not support certificate errors. The code I have got is from the python docs:

import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print r1.status, r1.reason

有谁知道如何连接到HTTPS?

Does anyone know how to connect to HTTPS?

我已经尝试过HTTPSConenction但是它响应了一个错误代码,声称httplib没有属性HTTPSConnection。我也没有socket.ssl可用。

I already tried the HTTPSConenction but it responds with an error code claiming httplib does not have attribute HTTPSConnection. I also don't have socket.ssl available.

我已经安装了Python 2.6.4,我认为它没有编译SSL支持。有没有办法将这个支持集成到较新的python中,而无需再次安装它。

I have installed Python 2.6.4 and I don't think it has SSL support compiled into it. Is there a way to integrate this suppot into the newer python without having to install it again.

我已经安装了OpenSSL和pyOpenSsl,我尝试了以下代码之一答案:

I have installed OpenSSL and pyOpenSsl and I have tried the below code from one of the answers:

import urllib2
from OpenSSL import SSL
try: 
    response = urllib2.urlopen('https://example.com')  
    print 'response headers: "%s"' % response.info() 
except IOError, e: 
    if hasattr(e, 'code'): # HTTPError 
        print 'http error code: ', e.code 
    elif hasattr(e, 'reason'): # URLError 
        print "can't connect, reason: ", e.reason 
    else: 
        raise

我有错误:

    Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/build/workspace/downloads/Python-2.6.4/Lib/urllib.py", line 87, in urlopen
    return opener.open(url)
  File "/home/build/workspace/downloads/Python-2.6.4/Lib/urllib.py", line 203, in open
    return self.open_unknown(fullurl, data)
  File "/home/build/workspace/downloads/Python-2.6.4/Lib/urllib.py", line 215, in open_unknown
    raise IOError, ('url error', 'unknown url type', type)
IOError: [Errno url error] unknown url type: 'https'

有谁知道如何让这个工作?

Does anyone know how to get this working?

-- UPDATE

我发现问题所在,我使用的Python版本没有SSL支持。我现在找到了这个解决方案: http://www.webtop.com .AU /编译的Python-与-SSL-支持

I have found out what the problem was, the Python version I was using did not have support for SSL. I have found this solution currently at: http://www.webtop.com.au/compiling-python-with-ssl-support.

此解决方案现在可以在此解决方案之后工作,这非常好。当我导入ssl和HTTPSConnection时,我知道不会出错。

The code will now work after this solution which is very good. When I import ssl and HTTPSConnection I know don't get an error.

感谢所有人的帮助。

推荐答案

Python 2.x: docs.python。 org / 2 / library / httplib.html

Python 2.x: docs.python.org/2/library/httplib.html:


注意:HTTPS支持仅在套接字模块编译时才可用SSL支持。

Note: HTTPS support is only available if the socket module was compiled with SSL support.

Python 3.x: docs.python.org/3/library/http.client.html

Python 3.x: docs.python.org/3/library/http.client.html:


注意HTTPS支持仅在Python支持SSL(通过ssl模块)编译时才可用。

Note HTTPS support is only available if Python was compiled with SSL support (through the ssl module).



#!/usr/bin/env python

import httplib
c = httplib.HTTPSConnection("ccc.de")
c.request("GET", "/")
response = c.getresponse()
print response.status, response.reason
data = response.read()
print data
# => 
# 200 OK
# <!DOCTYPE html ....

要验证SSL是否已启用,请尝试:

To verify if SSL is enabled, try:

>>> import socket
>>> socket.ssl
<function ssl at 0x4038b0>

这篇关于HTTPS连接Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆