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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
try:
from google.appengine.api import urlfetch
from cookie_handlers import CookiefulUrlfetch
except:
pass
try:
import httplib2
from cookie_handlers import CookiefulHttp
except:
pass
try:
import urllib2
except:
pass
class Urllib2Client(object):
def __init__(self, cookiejar):
cookiespy = urllib2.HTTPCookieProcessor(cookiejar)
self.opener = urllib2.build_opener(cookiespy)
def __call__(self, url, method, body, headers):
req = urllib2.Request(url, body, headers)
try:
resp = self.opener.open(req)
except socket.error, e:
self.log.error('SOCKET FAILURE: %s', e.fp.read())
raise MetawebError, 'failed contacting %s: %s' % (url, str(e))
except urllib2.HTTPError, e:
self._raise_service_error(url, e.code, e.info().type, e.fp.read())
for header in resp.info().headers:
self.log.debug('HTTP HEADER %s', header)
name, value = re.split("[:\n\r]", header, 1)
if name.lower() == 'x-metaweb-tid':
self.tid = value.strip()
return (resp, resp.read())
class Httplib2Client(object):
def __init__(self, cookiejar):
self.cookiejar = cookiejar
self.httpclient = CookiefulHttp(cookiejar=self.cookiejar)
def __call__(self, url, method, body, headers):
try:
resp, content = self.httpclient.request(url, method=method,
body=body, headers=headers)
if (resp.status != 200):
self._raise_service_error(url, resp.status, resp['content-type'], content)
except socket.error, e:
self.log.error('SOCKET FAILURE: %s', e.fp.read())
raise MetawebError, 'failed contacting %s: %s' % (url, str(e))
except httplib2.HttpLib2ErrorWithResponse, e:
self._raise_service_error(url, resp.status, resp['content-type'], content)
except httplib2.HttpLib2Error, e:
raise MetawebError(u'HTTP error: %s' % (e,))
#tid = resp.get('x-metaweb-tid', None)
return (resp, content)
class UrlfetchClient(object):
def __init__(self, cookiejar):
self.cookiejar = cookiejar
self.httpclient = CookiefulUrlfetch(cookiejar=self.cookiejar)
def __call__(self, url, method, body, headers):
resp = self.httpclient.request(url, payload=body, method=method, headers=headers)
if resp.status_code != 200:
self._raise_service_error(url, resp.status_code, resp.headers['content-type'], resp.body)
return (resp, resp.content)
|