forked from getsentry/sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplays.py
More file actions
48 lines (40 loc) · 1.29 KB
/
replays.py
File metadata and controls
48 lines (40 loc) · 1.29 KB
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
from httplib import HTTPConnection, HTTPSConnection
from urllib import urlencode
from urlparse import urlparse
import socket
class Replayer(object):
def __init__(self, url, method, data=None, headers=None):
self.url = url
self.method = method
self.data = data
self.headers = headers
def replay(self):
urlparts = urlparse(self.url)
if urlparts.scheme == 'http':
conn_cls = HTTPConnection
elif urlparts.scheme == 'https':
conn_cls = HTTPSConnection
else:
raise ValueError(self.url)
data = self.data
if isinstance(data, dict):
data = urlencode(data)
if urlparts.query:
full_url = urlparts.path + '?' + urlparts.query
else:
full_url = urlparts.path
conn = conn_cls(urlparts.netloc)
try:
conn.request(self.method, full_url, data, self.headers or {})
response = conn.getresponse()
except socket.error as e:
return {
'status': 'error',
'reason': str(e),
}
return {
'status': response.status,
'reason': response.reason,
'headers': response.getheaders(),
'body': response.read(),
}