gpt4 book ai didi

python - 在 Python 中获取 HTTP GET 参数

转载 作者:IT老高 更新时间:2023-10-28 22:24:49 28 4
gpt4 key购买 nike

我正在尝试使用简单的 Python 脚本运行 Icecast 流,以从服务器上的歌曲列表中选择一首随机歌曲。我希望添加一个投票/请求接口(interface),我的主机允许使用 python 通过 CGI 服务网页。但是,我对如何获取用户提供的 GET 参数感到困惑。我用 sys.argv 尝试了通常的方法:

#!/usr/bin/python
import sys
print "Content-type: text/html\n\n"
print sys.argv

但是打了http://example.com/index.py?abc=123&xyz=987只返回“['index.py']”。 Python 是否有其他一些功能可以用于此目的,或者我需要用 CGI 进行更改吗?我正在尝试做的事情是否可能?

谢谢。

最佳答案

cgi.FieldStorage() 应该为您解决问题...它返回一个字典,其中键作为字段,值作为其值。

import cgi
import cgitb; cgitb.enable() # Optional; for debugging only

print "Content-Type: text/html"
print ""

arguments = cgi.FieldStorage()
for i in arguments.keys():
print arguments[i].value

关于python - 在 Python 中获取 HTTP GET 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3582398/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com