Skip to content

Commit

Permalink
Merge pull request #13 from Selfnet/use/session
Browse files Browse the repository at this point in the history
Use/session
  • Loading branch information
Cellebyte authored Oct 4, 2018
2 parents a15c321 + e0587bb commit 802ac36
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 55 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ This object represents a keyserver using the hkp protocol.
Initialize the KeyServer object.

```python
# python2and3 compliance
from __future__ import absolute_import, unicode_literals, print_function

from hkp4py import KeyServer


Expand Down
73 changes: 20 additions & 53 deletions hkp4py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Key(object):
_end_header = '-----END PGP PUBLIC KEY BLOCK-----'

def __init__(self, host, port, keyid, algo, keylen,
creation_date, expiration_date, flags, proxies=None, headers=None, verify=None):
creation_date, expiration_date, flags, session=None):
"""
Takes keyserver host and port used to look up ASCII armored key, and
data as it is present in search query result.
Expand All @@ -54,9 +54,7 @@ def __init__(self, host, port, keyid, algo, keylen,
self.algo = ALGORITHMS.get(algo, algo)
self.key_length = int(keylen)
self.creation_date = datetime.fromtimestamp(int(creation_date))
self.proxies = proxies
self.headers = headers
self.verify = verify
self.session = session

if expiration_date:
self.expiration_date = datetime.fromtimestamp(int(expiration_date))
Expand Down Expand Up @@ -102,8 +100,8 @@ def retrieve(self, nm=False, blob=False):
'options': ','.join(name for name, val in opts if val),
}
request_url = '{}:{}/pks/lookup'.format(self.host, self.port)
response = requests.get(
request_url, params=params, proxies=self.proxies, headers=self.headers, verify=self.verify)
response = self.session.get(
request_url, params=params)
if response.ok:
# strip off enclosing text or HTML. According to RFC headers MUST be
# always preserved, so we rely on them
Expand All @@ -114,7 +112,7 @@ def retrieve(self, nm=False, blob=False):
if blob:
# cannot use requests.content because of potential html
# provided by keyserver. (see above comment)
return key.encode("utf-8")
return bytes(key.encode("utf-8"))
else:
return key
else:
Expand Down Expand Up @@ -161,21 +159,23 @@ class KeyServer(object):
Keyserver object used for search queries.
"""

def __init__(self, host, port=11371, proxies=None, headers=None, verify=True, ca=None):
def __init__(self, host, port=11371, proxies=None, headers=None, verify=True):
if host.startswith('hkp://') or host.startswith('hkps://'):
host = host.replace("hkp", "http", 1)
if host.startswith('https'):
if port == 11371:
port = 443
else:
raise Exception("Unsupported protocol, hkp|hkps are supported.")

self.host = host
self.port = port
self.proxies = proxies
self.headers = headers
self.verify = verify
self.ca = ca or utils.ca()
# Buildup Session
self.session = requests.session()
self.session.headers = headers
self.session.proxies = proxies
if host.endswith("hkps.pool.sks-keyservers.net"):
verify = utils.ca().pem
self.session.verify = verify

def __parse_index(self, response):
"""
Expand All @@ -188,7 +188,7 @@ def __parse_index(self, response):
items = line.split(':')
if 'pub' in items[0]:
key = Key(self.host, self.port, *
items[1:], proxies=self.proxies, headers=self.headers, verify=self.verify)
items[1:], session=self.session)
result.append(key)
if 'uid' in items[0] and key:
key.identities.append(Identity(*items[1:]))
Expand All @@ -210,25 +210,9 @@ def search(self, query, exact=False, nm=False):
}

request_url = '{}:{}/pks/lookup'.format(self.host, self.port)
try:
response = requests.get(
request_url,
params=params,
proxies=self.proxies,
headers=self.headers,
verify=self.verify
)
except:
if self.verify == self.ca.pem:
raise
self.verify = self.ca.pem
response = requests.get(
request_url,
params=params,
proxies=self.proxies,
headers=self.headers,
verify=self.verify
)
response = self.session.get(
request_url,
params=params)
if response.ok:
response = response.text
else:
Expand All @@ -241,24 +225,7 @@ def add(self, key):
"""
request_url = '{}:{}/pks/add'.format(self.host, self.port)
data = {'keytext': key}
try:
response = requests.post(
request_url,
data=data,
proxies=self.proxies,
headers=self.headers,
verify=self.verify
)
except:
# check if the ca was already set
if self.verify == self.ca.pem:
raise
self.verify = self.ca.pem
response = requests.post(
request_url,
data=data,
proxies=self.proxies,
headers=self.headers,
verify=self.verify
)
response = self.session.post(
request_url,
data=data)
response.raise_for_status()
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
with io.open(path.join(HERE, 'README.md'), encoding='utf-8') as f:
LONG_DESCRIPTION = f.read()

__version__ = "0.2.2.0"
__version__ = "0.2.3.0"

setup(
name='hkp4py',
Expand Down Expand Up @@ -68,7 +68,8 @@

# You can just specify the packages manually here if your project is
# simple. Or you can use find_packages().
packages=find_packages(exclude=['bin', 'lib', 'contrib', 'docs', 'tests', 'dist', 'env', 'env2']),
packages=find_packages(
exclude=['bin', 'lib', 'contrib', 'docs', 'tests', 'dist', 'env', 'env2']),

# Alternatively, if you want to distribute just a my_module.py, uncomment
# this:
Expand Down

0 comments on commit 802ac36

Please sign in to comment.