我如何在python

时间:2016-02-23 16:47:05

标签: python

我需要在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

时执行的代码

我需要,当我访问“http://localhost:8081/?cid=5&aid=4”时 得到cid和援助价值

1 个答案:

答案 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()