tags:

views:

104

answers:

1
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

This code enable me to see some webpage information and contents. what actually i had query that how to fetch the public key from any public key server using python function.

+1  A: 

This spec should help you along, and stop you asking the same question repeatedly :). Basically you need to perform an HTTP GET request to the key server. Most key servers work on TCP port 11371 so you need to ensure that your firewall will let you through.

For example, this URL will give you God's public key:

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

The response is a HTML page containing the key and you will have to extract the key from the unnecessary HTML wrapping.

In python:

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

<html><head><title>Public Key Server -- Get ``0x1278a1862492d908 ''</title></head>
<body><h1>Public Key Server -- Get ``0x1278a1862492d908 ''</h1>
<pre>
-----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/VHPjB7ZSdshl9GM86TXYnWj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