python中的web server 和 request …

WEB SERVER
1 标准库中wsgiref.simple_server(由于该server只能支持单线程,因此一般只作测试之用)
from wsgiref.simple_server import make_server
server = make_server('127.0.0.1', 8080, app)
server.serve_forever()
2 paste的httpserver:
def app(environ, start_response):
    start_response('200 OK', [('content-type', 'text/html')])
    return ['Hello world!']


if
__name__ == '__main__':
    from paste import httpserver
    httpserver.serve(app, host='127.0.0.1', port='8080')

request请求方式
1 httplib.HTTPConnection请求:
httplib请求方式1:
monitor_conn = httplib.HTTPConnection(ip)
monitor_conn.request("POST", path, body=body_str, headers=headers)
monitor_response = monitor_conn.getresponse()
response_data = monitor_response.read()

httplib请求方式2:
monitor_conn = httplib.HTTPConnection(ip)
monitor_conn.putrequest('POST', path)
for header, value in kwargs['headers'].items():
    conn.putheader(header, value)
conn.endheaders()
chunk = kwargs['body'].read(CHUNKSIZE)  # 用于传输大文件用的,分块传输
while chunk:
    conn.send('%x\r\n%s\r\n' % (len(chunk), chunk))
    chunk = kwargs['body'].read(CHUNKSIZE)
conn.send('0\r\n\r\n')
monitor_reponse = monitor_conn.getresponse()
reponse_data = monitor_response.read()
2 urllib2.Request
request = urllib2.Request(url) or None
[request.add_header(i, j) for i, j in self.headers]
request.add_data(json.dumps(data))
response = urllib2.urlopen(request)
res=response.read()
3 webob.Request d
def __call__(self, environ, start_response):
   req = Request(environ)
   resp = req.get_response(self.app)
   resp.body = resp.body.upper()
return resp(environ, start_response)
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页