2011-11-08 143 views
6

我有提供自签名证书,我产生这样的HTTPS连接龙卷风服务器:HTTPS Python客户端

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为原型)?

谢谢。

+0

如果你能为我提供一个Java的客户端也是可以的。 – lc2817

+0

相关http://www.heikkitoivonen.net/blog/2010/08/23/ssl-in-python-2-7/ – jfs

+0

相关:http://stackoverflow.com/questions/1087227/validate-ssl-certificates -with-python – jfs

回答

3

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

它很好地解释了here for an Android app

+0

谢谢,你能给我更多关于如何在iPhone应用上做到这一点的信息吗? – lc2817

1

客户端没有办法确保服务器说出真相。您可以为google.com创建自签名证书。

+0

假设我可以在证书颁发机构注册我的证书,那么我应该怎么做? – lc2817

+0

@ lc2817:那么你可以使用[其中一个答案](我之前已经链接过了)(http://stackoverflow.com/questions/1087227/validate-ssl-certificates-with-python)。 – jfs

+0

您没有回答我对其他链接的最新评论。 – lc2817