我需要在Python中获取url的参数:
from wsgiref.simple_server import make_server, demo_app
def showresult(environ, start_response):
status = '200 OK' # HTTP Status
headers = [('Content-type', 'application/json')] # HTTP Headers
start_response(status, headers)
# The returned object is going to be printed
return "ok"
httpd = make_server('', 8081, showresult)
# Respond to requests until process is killed
httpd.serve_forever()
时执行的代码
我需要,当我访问“http://localhost:8081/?cid=5&aid=4”时 得到cid和援助价值
答案 0 :(得分:2)
实现这一目标的一种简单方法是使用urlparse.parse_qs
from urlparse import parse_qs
from wsgiref.simple_server import make_server, demo_app
def showresult(environ, start_response):
status = '200 OK' # HTTP Status
headers = [('Content-type', 'application/json')] # HTTP Headers
start_response(status, headers)
params = parse_qs(environ['QUERY_STRING']) # Here you get the values in a dict!
print params
# The returned object is going to be printed
return "ok"
httpd = make_server('', 8081, showresult)
# Respond to requests until process is killed
httpd.serve_forever()