-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackets.py
More file actions
68 lines (45 loc) · 1.68 KB
/
Copy pathpackets.py
File metadata and controls
68 lines (45 loc) · 1.68 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#coding: utf8
try:
import urllib2
except ImportError:
import urllib.request as urllib2
__author__ = 'AlOsipov'
class Packet(object):
DEFAULT_MIME = 'application/xml'
DEFAULT_HEADERS = {'Access-Control-Allow-Origin': '*'}
def __init__(self, content='', status=200, mimetype=None, headers=None, force_headers=True):
self.content = content
self.status = status
self.mimetype = mimetype
self.headers = headers
if self.mimetype is None:
self.mimetype = Packet.DEFAULT_MIME
if self.headers is None:
self.headers = dict(Packet.DEFAULT_HEADERS)
if isinstance(self.headers, dict) and force_headers:
self.headers.update(Packet.DEFAULT_HEADERS)
self._header_keys_list = None
def _header_keys(self):
if self._header_keys is not None:
self._header_keys_list = set(
map(lambda x: x.upper(), dict(self.headers).keys())
)
return self._header_keys_list
def _create_headers(self):
tmp_result = []
if 'CONTENT-TYPE' not in self._header_keys():
tmp_result.append('Content-type: {0}'.format(self.mimetype))
header_list = zip(self.headers.keys(), self.headers.values())
for key, value in header_list:
tmp_result.append('{0}: {1}'.format(
key, value
))
return tmp_result
def __str__(self):
base = list()
base.append('HTTP/1.1 {0} OK'.format(self.status))
base.extend(self._create_headers())
base.append('')
base.append(self.content)
tmp_result = "\r\n".join(base)
return tmp_result