HTTPS Python客户端
我有一个tornado服务器,它提供了一个https连接,带有我通过以下方式生成的自签名证书:HTTPS Python客户端,python,mobile,https,certificate,tornado,Python,Mobile,Https,Certificate,Tornado,我有一个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
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()
服务器的代码如下所示:
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 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()
import httplib
HOSTNAME='localhost'
conn = httplib.HTTPSConnection(HOSTNAME)
conn.putrequest('GET','/')
conn.endheaders()
response = conn.getresponse()
print response.read()
您建议我怎么做(客户端稍后将成为移动应用程序我只使用python进行原型制作)
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()
谢谢。客户端无法确保服务器说出了真相。您可以为google.com创建自签名证书。如果您也控制客户端(如在android或iphone应用程序中),您可以将自签名证书添加到受信任的证书存储中
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()
这是很好的解释如果你能用Java为我提供一个同样可以的客户端。related:@J.F.Sebastian我已经看到了这个答案,但我不知道如何使它适应我的案例相关:假设我可以在证书颁发机构注册我的证书,那么我该怎么办?@lc2817:那么你可以使用我之前链接过的。你呢没有回答我上次对其他链接的评论。@lc2817:如果您的证书在客户端本地可用,则可以将其添加为受信任证书(CA)。这些链接包含示例(
ca_certs
用于ssl
;store。添加_cert()
用于使用Twisted的答案,CAINFO
用于pycurl
)。另一个例子:(使用ca_certs
将您的自签名证书添加到受信任列表中)。非常感谢,我将尝试此方法。谢谢,您能给我更多关于如何在IPhone应用程序上执行此操作的信息吗?
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()