forked from orisi/orisi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliburl_wrapper.py
37 lines (31 loc) · 1.05 KB
/
liburl_wrapper.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
import signal
import socket
import urllib2
import urllib
TIMEOUT = 10
socket.setdefaulttimeout(TIMEOUT)
def timeout_catcher(signum, _):
raise urllib2.URLError("Read timeout")
signal.signal(signal.SIGALRM, timeout_catcher)
def safe_read(url, timeout_time):
signal.setitimer(signal.ITIMER_REAL, timeout_time)
try:
content = urllib2.urlopen(url, timeout=timeout_time).read()
signal.setitimer(signal.ITIMER_REAL, 0)
return content
except:
signal.setitimer(signal.ITIMER_REAL, 0)
return None
def safe_pushtx(tx, timeout_time = 10):
signal.setitimer(signal.ITIMER_REAL, timeout_time)
try:
#thanks http://www.pythonforbeginners.com/python-on-the-web/how-to-use-urllib2-in-python/
query_args = {'send': 'Push', 'transaction': tx}
data = urllib.urlencode(query_args)
url = 'http://eligius.st/~wizkid057/newstats/pushtxn.php'
content = urllib2.urlopen(url, data, timeout=timeout_time).read()
signal.setitimer(signal.ITIMER_REAL, 0)
return content
except:
signal.setitimer(signal.ITIMER_REAL, 0)
return None