获取HTTP在Python GET参数 [英] Getting HTTP GET arguments in Python
问题描述
我试图运行使用一个简单的Python脚本来接从服务器上的歌曲列表中随机播放歌曲的Icecast一个流。我期待添加一个投票/请求接口,以及我的主人允许使用Python的CGI,通过服务网页。不过,我得到的只是如何让用户提供的GET参数挂断了电话。我试着用通常的方法用sys.argv中:
I'm trying to run an Icecast stream using a simple Python script to pick a random song from the list of songs on the server. I'm looking to add a voting/request interface, and my host allows use of python to serve webpages through CGI. However, I'm getting hung up on just how to get the GET arguments supplied by the user. I've tried the usual way with sys.argv:
#!/usr/bin/python
import sys
print "Content-type: text/html\n\n"
print sys.argv
但打了<一个href=\"http://example.com/index.py?abc=123&xyz=987\">http://example.com/index.py?abc=123&xyz=987仅返回['index.py']。有一些其他的功能Python有此目的,或者是有什么我需要CGI改变?正是我试图做甚至可能吗?
But hitting up http://example.com/index.py?abc=123&xyz=987 only returns "['index.py']". Is there some other function Python has for this purpose, or is there something I need to change with CGI? Is what I'm trying to do even possible?
感谢。
推荐答案
cgi.FieldStorage()应该为你做的伎俩...
它返回一个字典,键为字段和值,因为它的价值。
cgi.FieldStorage() should do the trick for you... it returns a dictionary with key as the field and value as it's value.
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
这篇关于获取HTTP在Python GET参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!