有关如何从公钥服务器获取公钥的相关查询

时间:2009-08-20 05:32:19

标签: python urllib

import urllib

response = urllib.urlopen('http://pool.sks-keyservers.net/')
print 'RESPONSE:', response
print 'URL     :', response.geturl()

headers = response.info()
print 'DATE    :', headers['date']
print 'HEADERS :'
print '---------'
print headers

data = response.read()
print 'LENGTH  :', len(data)
print 'DATA    :'
print '---------'
print data

此代码使我能够看到一些网页信息和内容。 实际上我查询了如何使用python函数从任何公钥服务器获取公钥。

2 个答案:

答案 0 :(得分:4)

This spec应该帮助你,并阻止你反复问同样的问题:)。基本上,您需要对密钥服务器执行HTTP GET请求。大多数关键服务器都在TCP端口11371上工作,因此您需要确保防火墙能够让您通过。

例如,此URL将为您提供上帝的公钥:

http://pool.sks-keyservers.net:11371/pks/lookup?op=get&search=0x1278A1862492D908&options=mr

回应是关键。

在python中:

import urllib
f = urllib.urlopen('http://pool.sks-keyservers.net:11371/pks/lookup?op=get&search=0x1278A1862492D908&options=mr')
data = f.read()
print data

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: SKS 1.1.0

mQGiBD/dywoRBADkaddBEedMhFHGH3wKORuOIDFufSDERmlm2ktj3ma+GhfwvnuvvpAj+QYl
ANh1K86Mm5k0dGOlhZwIr1zB3cIoNqt7TJ62v8w6mc0BA8UWzWJp0i6dHPa/qeeFFC53B8U1
h3FlPrmQGcVeV+hjOPFU7ANDSDm3tduad7NRxAst7QCg/8o++2BaAlTnbMB+Xfo23uEEc6UE
AK0vD4EUPLfU5snfow1zUPXQUDalOcUP6RIhbi6yxKRFAIWI+7QgNPZf/Q2CFIRWsXKmW/ly
IDSJgs5ruB+Yj8gmBZlrn5KMmW3EcEoHAhOP+ONZAIOb2LsaAjHjHOuefhlIr1T3gng+kLoD
3Yfy2WTugBizeNybAd2nfZyOJhfWA/4xTfg+Hbcb9n+8sEqgiuyQJEID38Q3FDmwSzRfBMbO
JFrf8t/VHPjB7ZSdshl9GM86TXYnWjspzAjjQB8fxLgIim2mC0T46aRNdE0l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BLOCK-----

答案 1 :(得分:1)

使用http://pypi.python.org/pypi/python-hkp

Python HPK客户端。

用法示例:

>>> from hkp import KeyServer
>>> serv = KeyServer('http://pool.sks-keyservers.net')
>>> serv.search('Dmitry Gladkov')
[Key 28DFA7EC RSA (Encrypt or Sign), Key 473C57D9 RSA (Encrypt or Sign)]
>>> serv.search('Dmitry Gladkov')[0].identities
[Identity Dmitry Gladkov (dgl) <dmitry.gladkov@gmail.com>]