fixed deadlock bug
[p2prfd.git] / http_helper.py
blob9e92710aee7e995629b9cbb439678587b10546bb
1 """helper functions related to HTTP"""
3 import httplib
4 import urllib
6 def http_get(target, queries):
7 print 'AWAL MINTA GET ke %s queries %s' % (repr(target), repr(queries))
8 print 'before get connection'
9 http_con = httplib.HTTPConnection(target[0], target[1])
10 print 'after get connection'
11 print 'before get request'
12 http_con.request("GET", '/?' + urllib.urlencode(queries))
13 print 'after get request'
14 print 'before get getresponse'
15 response = http_con.getresponse()
16 print 'after get getresponse'
17 resp_str = response.read()
18 result = eval(resp_str)
19 http_con.close()
20 print 'AKHIR MINTA GET ke %s queries %s' % (repr(target), repr(queries))
21 return result
22 def http_post(target, queries):
23 print 'AWAL MINTA POST ke %s queries %s' % (repr(target), repr(queries))
24 print 'before post connection'
25 http_con = httplib.HTTPConnection(target[0], target[1])
26 print 'after post connection'
27 headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
28 body = urllib.urlencode(queries)
29 print 'before post request'
30 http_con.request("POST", '/', body, headers)
31 print 'after post request'
32 print 'before post getresponse'
33 response = http_con.getresponse()
34 print 'after post getresponse'
35 resp_str = response.read()
36 result = eval(resp_str)
37 http_con.close()
38 print 'AKHIR MINTA POST ke %s queries %s' % (repr(target), repr(queries))
39 return result