-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxhttp_utils.py
More file actions
41 lines (35 loc) · 1.22 KB
/
xhttp_utils.py
File metadata and controls
41 lines (35 loc) · 1.22 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
#!/usr/bin/python
# -*- coding:utf-8 -*-
import httplib
class ServerResponse(object):
def __init__(self,code,headers,body):
self.code = code
self.headers = headers
self.body = body
def pack_http_response_buf(self):
status = httplib.responses.get(self.code)
if not status:
status = "Unknown"
segs = []
segs.append("HTTP/1.1 %u %s" % (self.code,status))
for key,value in self.headers.iteritems():
segs.append("%s:%s"%(key,value))
if self.body == None:
body = status
else:
body = str(self.body)
segs.append("Content-Length:%d" % len(body))
message = "%s\r\n\r\n%s" % ("\r\n".join(segs),body)
return message
def render_to_response(body,code=200,headers={}):
return ServerResponse(code,headers,body)
def pack_code_response(code,headers={}):
return render_to_response("",code,headers)
def login_required(auth_func):
def _deco(handler):
def __deco(request):
if auth_func(request):
return handler(request)
return render_to_response("",401,{"WWW-Authenticate":'Basic realm="insert realm"'})
return __deco
return _deco