forked from RGPaul/script.traktutilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbhttpsconnection.py
65 lines (53 loc) · 1.71 KB
/
nbhttpsconnection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
#
import os, sys
import time, socket
import urllib
import thread
import threading
try:
# Python 3.0 +
import http.client as httplib
except ImportError:
# Python 2.7 and earlier
import httplib
try:
# Python 2.6 +
from hashlib import sha as sha
except ImportError:
# Python 2.5 and earlier
import sha
__author__ = "Ralph-Gordon Paul, Adrian Cowan"
__credits__ = ["Ralph-Gordon Paul", "Adrian Cowan", "Justin Nemeth", "Sean Rudford"]
__license__ = "GPL"
__maintainer__ = "Ralph-Gordon Paul"
__email__ = "ralph-gordon.paul@uni-duesseldorf.de"
__status__ = "Production"
# Allows non-blocking http requests
class NBHTTPSConnection():
def __init__(self, host, port = None, strict = None, timeout = None):
self.rawConnection = httplib.HTTPSConnection(host, port, strict, timeout)
self.responce = None
self.responceLock = threading.Lock()
self.closing = False
def request(self, method, url, body = None, headers = {}):
self.rawConnection.request(method, url, body, headers);
def hasResult(self):
if self.responceLock.acquire(False):
self.responceLock.release()
return True
else:
return False
def getResult(self):
while not self.hasResult() and not self.closing:
time.sleep(1)
return self.responce
def go(self):
self.responceLock.acquire()
thread.start_new_thread ( NBHTTPSConnection._run, ( self, ) )
def _run(self):
self.responce = self.rawConnection.getresponse()
self.responceLock.release()
def close(self):
self.closing = True
self.rawConnection.close()