正在从 cgi.FieldStorage 中检索完整的 URL

Retrieving full URL from cgi.FieldStorage

我正在使用 cgi.FieldStorage():

将 URL 传递给 python 脚本
http://localhost/cgi-bin/test.py?file=http://localhost/test.xml

test.py 刚好包含

#!/usr/bin/env python

import cgi
print "Access-Control-Allow-Origin: *"
print "Content-Type: text/plain; charset=x-user-defined"
print "Accept-Ranges: bytes"
print
print cgi.FieldStorage()

结果是

FieldStorage(None, None, [MiniFieldStorage('file', 'http:/localhost/test.xml')])

请注意,URL 仅包含 http:/localhost - 如何传递完整的编码 URI,以便文件是整个 URI?我试过对文件参数进行编码 (http%3A%2F%2Flocalhost%2ftext.xml),但这也不起作用

屏幕截图显示网页的输出不是预期的,但编码的 url 是正确的

问题出在您的查询参数上,您应该对它们进行编码:

>>> from urllib import urlencode
>>> urlencode({'file': 'http://localhost/test.xml', 'other': 'this/has/forward/slashes'})
'other=this%2Fhas%2Fforward%2Fslashes&file=http%3A%2F%2Flocalhost%2Ftest.xml'

我使用 Apache 2.4.10 和 Firefox(也包括 curl)时,您的 CGI 脚本工作正常。您使用的是什么网络服务器和浏览器?

我的猜测是您正在使用 Python 的 CGIHTTPServer 或基于它的东西。这显示了您确定的问题。 CGIHTTPServer 假定为它提供了 CGI 脚本的路径,因此它会折叠路径而不考虑可能存在的任何查询字符串。折叠路径会删除重复的正斜杠以及相对路径元素,例如 ...

如果您正在使用此 Web 服务器,我看不到通过更改 URL 有任何明显的解决方法。你不会在生产中使用它,所以也许看看另一个网络服务器,如 Apache、nginx、lighttpd 等。