使用python访问具有PKI安全性的站点 [英] Use python to access a site with PKI security
问题描述
我有一个启用了 PKI 安全性的站点.每个客户端使用读卡器加载他们的证书,或者证书安装在他们盒子上的 IE 证书存储中.
I have a site that has PKI security enabled. Each client used either a card reader to load their certificate, or the certificate is installed in the IE certificate storage on their box.
所以我的问题是:
- 如何使用读卡器证书或系统中存储的证书来验证系统?
- 如何将凭据传递到网站上说,嘿,我是我,我可以访问该服务?他们的例子可以是使用软证书.我可以稍后弄清楚读卡器部分.
我一直在四处寻找,但在这种情况下我没有想出任何可以帮助我的东西.Django 有一堆模块,但这不是一个选项,因为我只关心客户端的事情.我没有创建一个站点来托管该服务.我只需要访问这些服务.
I've been searching around, and I haven't come up with anything to help me in this situation. Django has a bunch of modules, but this isn't an option because I'm only concerned of the client side of things. I'm not creating a site to host the service. I need to just access these services.
我有这段代码可以工作.我只是不知道如何处理我得到的重定向:
I have this code working sort of. I just do not know how to handle the redirect I am getting:
import httplib
KEYFILE = r"C:\cert\my.key"
CERTFILE = r"c:\cert\my.pem"
HOSTNAME = 'machine.com'
conn = httplib.HTTPSConnection(
HOSTNAME,
key_file = KEYFILE,
cert_file = CERTFILE
)
conn.putrequest('GET', '/arcgis/sharing/rest?f=json')
conn.endheaders()
response = conn.getresponse()
print response.read()
这一切的结果是:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://machine.com/pki?https://machine.com/arcgis/sharing/rest%3f&f=json">here</a>.</p>
</body></html>
提供的任何帮助都会很棒!
Any help provided would be great!
软件规格:python 2.7.8,Windows 2012 R2
Software specs: python 2.7.8, Windows 2012 R2
推荐答案
我创建了一个 PKI 处理程序来处理请求,以便我可以使用它来工作 urllib2 库.
I created a PKI handler to handle the requests so I can use it work urllib2 library.
import httplib, urllib2
class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
def __init__(self, key, cert):
urllib2.HTTPSHandler.__init__(self)
self.key = key
self.cert = cert
def https_open(self, req):
#Rather than pass in a reference to a connection class, we pass in
# a reference to a function which, for all intents and purposes,
# will behave as a constructor
return self.do_open(self.getConnection, req)
def getConnection(self, host, timeout=300):
return httplib.HTTPSConnection(host,
key_file=self.key,
cert_file=self.cert,
timeout=timeout)
要使用它,您需要使用带有处理程序的 cookiejar.
To use this, you will need to use a cookiejar with the handler.
from cookielib import CookieJar
cookiejar = CookieJay()
handlers = []
handlers.append(HTTPSClientAuthHandler(somekey, somecert))
handlers.append(urllib2.HTTPCookieProcessor(cookiejar))
opener = urllib2.build_opener(*handlers)
... do other urllib2 calls ....
希望对大家有帮助!
这篇关于使用python访问具有PKI安全性的站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!