get方法
代码实现
import json
from urlparse import parse_qs
from wsgiref.simple_server import make_server
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
params = parse_qs(environ['QUERY_STRING'])
name = params.get('name', [''])[0]
no = params.get('no', [''])[0]
dic = {'name': name, 'no': no}
return [json.dumps(dic)]
if __name__ == "__main__":
port = 5088
httpd = make_server("0.0.0.0", port, application)
print "serving http on port {0}...".format(str(port))
httpd.serve_forever()
请求实例
post方法
代码实现
import json
from wsgiref.simple_server import make_server
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'application/json')])
request_body = environ["wsgi.input"].read(int(environ.get("CONTENT_LENGTH", 0)))
request_body = json.loads(request_body)
name = request_body["name"]
no = request_body["no"]
dic = {'myNameIs': name, 'myNoIs': no}
return [json.dumps(dic)]
if __name__ == "__main__":
port = 6088
httpd = make_server("0.0.0.0", port, application)
print "serving http on port {0}...".format(str(port))
httpd.serve_forever()
请求实例
疑问
- 怎么实现请求的路径限制?
- 怎么限制接口调用方的headers?
以上待研究,未完待续。。。