如何在python中获取url的参数

2024-10-03 09:19:43 发布

您现在位置: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和aid值


Tags: tolocalhosthttpmakeserverisresponsestatus
1条回答
网友
1楼 · 发布于 2024-10-03 09:19:43

实现这一点的一个简单方法是使用^{}

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

相关问题 更多 >