在 Python 中获取 HTTP GET 参数

发布于 2024-09-16 12:39:32 字数 486 浏览 4 评论 0原文

我正在尝试使用简单的 Python 脚本运行 Icecast 流,以从服务器上的歌曲列表中随机选择一首歌曲。我希望添加一个投票/请求接口,并且我的主机允许使用 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 进行更改吗?我想做的事情可能吗?

谢谢。

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

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?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给妤﹃绝世温柔 2024-09-23 12:39:32

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

cgi.FieldStorage() should do the trick for you... It returns a dictionary with key as the field and value as its 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
夏日浅笑〃 2024-09-23 12:39:32

对于 GET 请求,我更喜欢 cgi.parse()。它返回一个简单的列表字典。

import cgi
args = cgi.parse()

例如,查询字符串 ?key=secret&a=apple 被解析为:

{'key': ['secret'], 'a': ['apple']}

For GET requests I prefer cgi.parse(). It returns a simple dictionary of lists.

import cgi
args = cgi.parse()

For example, the query string ?key=secret&a=apple is parsed as:

{'key': ['secret'], 'a': ['apple']}
会傲 2024-09-23 12:39:32

鉴于 CGI 模块自 Python 3.11 起已弃用,并将在 3.13 中删除 ,这个问题是“python3获取cgi参数”的Google搜索结果之一,这里是使用建议的替换的示例(urllib.parse):

#!/usr/bin/python3

## import the required libraries
import os
import urllib.parse

## print a HTTP content header
print('Content-type: text/plain\r\n')

## get the query string. this gets passed to cgi scripts as the environment
## variable QUERY_STRING
query_string = os.environ['QUERY_STRING']

## convert the query string to a dictionary
arguments = urllib.parse.parse_qs(query_string)

## print out the values of each argument
for name in arguments.keys():
    ## the value is always a list, watch out for that
    print(str(name) + ' = ' + str(arguments[name]))

此脚本 假设您的 Python 3 安装位于 /usr/bin/python3 。您需要针对非 Linux 平台进行调整。

应该注意的是,以这种方式解析将为您提供列表形式的值。除非多次传递相同的参数,否则该列表将只有一个值。

例如,如果您在 http://192.168.0.1/script.cgi 托管上述 CGI 脚本:

请求 http ://192.168.0.1/script.cgi?hello=world&foo=bar< /em> 将向

hello = ['world']
foo = ['bar']

http ://192.168.0.1/script.cgi?hello=world&foo=bar&hello=now< /em> 将给出:

hello = ['world', 'now']
foo = ['bar']

Given that the CGI module is depreceated as of Python 3.11 and will be removed in 3.13, and this question is one of the top Google results for "python3 get cgi parameters" here is an example using the proposed replacement (urllib.parse):

#!/usr/bin/python3

## import the required libraries
import os
import urllib.parse

## print a HTTP content header
print('Content-type: text/plain\r\n')

## get the query string. this gets passed to cgi scripts as the environment
## variable QUERY_STRING
query_string = os.environ['QUERY_STRING']

## convert the query string to a dictionary
arguments = urllib.parse.parse_qs(query_string)

## print out the values of each argument
for name in arguments.keys():
    ## the value is always a list, watch out for that
    print(str(name) + ' = ' + str(arguments[name]))

This script assumes that your Python 3 install is located at /usr/bin/python3. You will need to adjust this for non-Linux platforms.

It should be noted that parsing in this way will give you values as a list. Unless you pass the same parameter multiple times, this list will only have one value.

For example, if you are hosting the above CGI script at http ://192.168.0.1/script.cgi:

A request to http ://192.168.0.1/script.cgi?hello=world&foo=bar will give

hello = ['world']
foo = ['bar']

A request to http ://192.168.0.1/script.cgi?hello=world&foo=bar&hello=now will give:

hello = ['world', 'now']
foo = ['bar']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文