gpt4 book ai didi

python - 使用python访问具有PKI安全性的站点

转载 作者:太空狗 更新时间:2023-10-29 17:50:34 24 4
gpt4 key购买 nike

我有一个启用了 PKI 安全性的站点。每个客户要么使用读卡器加载他们的证书,要么将证书安装在他们盒子上的 IE 证书存储中。

所以我的问题是:

  1. 如何使用读卡器证书或存储在系统中的证书来验证系统?
  2. 我如何将凭据传递到站点以表示,嘿,我是我,我可以访问该服务?他们的例子可以使用软证书。我可以稍后弄清楚读卡器部分。

我一直在四处寻找,但在这种情况下我没有想出任何可以帮助我的东西。 Django 有很多模块,但这不是一个选项,因为我只关心客户端的事情。我没有创建站点来托管该服务。我只需要访问这些服务。

我有这样的代码工作。我只是不知道如何处理我收到的重定向:

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&amp;f=json">here</a>.</p>
</body></html>

提供的任何帮助都会很棒!

软件规范:python 2.7.8,Windows 2012 R2

最佳答案

我创建了一个 PKI 处理程序来处理请求,因此我可以使用它来工作 urllib2 库。

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。

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 - 使用python访问具有PKI安全性的站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30666244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com