【问题标题】:HTTPS Python clientHTTPS Python 客户端
【发布时间】:2011-12-24 03:31:08
【问题描述】:

我有一个 tornado 服务器,它提供带有我以这种方式生成的自签名证书的 https 连接:

openssl genrsa -out privatekey.pem 1024                                         
openssl req -new -key privatekey.pem -out certrequest.csr 
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem

服务端代码如下:

import tornado.ioloop
import tornado.web
import tornado.httpserver
import os

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        print "new client "+str(self)
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])


http_server = tornado.httpserver.HTTPServer(application,
                                            ssl_options={
        "certfile": os.path.join("./", "certificate.pem"),
        "keyfile": os.path.join("./", "privatekey.pem"),

})

if __name__ == "__main__":
    http_server.listen(443)
    tornado.ioloop.IOLoop.instance().start()

我想要一个 python 客户端连接到服务器并检查服务器是否是正确的服务器(我猜是通过它的证书)。 目前我做了一个这样的简单客户端:

import httplib
HOSTNAME='localhost'
conn = httplib.HTTPSConnection(HOSTNAME)
conn.putrequest('GET','/')
conn.endheaders()
response = conn.getresponse()
print response.read()

你会建议我做什么(客户端稍后将成为移动应用程序我只使用 python 进行原型设计)?

谢谢。

【问题讨论】:

标签: python mobile https certificate tornado


【解决方案1】:

如果您也控制客户端(例如在 android 或 iphone 应用程序中),您可以将您的自签名证书添加到您信任的证书存储中。

很好解释here for an Android app

【讨论】:

  • 谢谢,你能给我更多关于如何在 iPhone 应用上做的信息吗?
【解决方案2】:

客户端无法确保服务器说的是真话。您可以为 google.com 创建自签名证书。

【讨论】:

  • 假设我可以在证书颁发机构注册我的证书,那我该怎么办?
  • @lc2817:那么你可以使用one of the answers我之前链接过。
  • 你没有回答我对其他链接的最后评论。
  • @lc2817:如果您的证书在客户端本地可用,那么您可以将其添加为受信任的证书 (CA)。链接包含示例(ca_certs 代表 sslstore.add_cert() 代表使用 Twisted 的答案,CAINFO 代表 pycurl)。另一个示例:urllib2_ssl.py(使用ca_certs 将您的自签名证书添加到受信任列表中)。
  • 非常感谢,我试试这个。
猜你喜欢
  • 2017-12-06
  • 1970-01-01
  • 2017-01-02
  • 2012-06-19
  • 1970-01-01
  • 2016-02-21
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多