简体   繁体   English

从cgi.FieldStorage检索完整的URL

[英]Retrieving full URL from cgi.FieldStorage

I'm passing a URL to a python script using cgi.FieldStorage() : 我正在使用cgi.FieldStorage()将URL传递给python脚本:

http://localhost/cgi-bin/test.py?file=http://localhost/test.xml

test.py just contains 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()

and the result is 结果是

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

Note that the URL only contains http:/localhost - how do I pass the full encoded URI so that file is the whole URI? 请注意,URL仅包含http:/localhost我如何传递完整的编码URI,以便该文件是整个URI? I've tried encoding the file parameter ( http%3A%2F%2Flocalhost%2ftext.xml ) but this also doesn't work 我尝试对文件参数( http%3A%2F%2Flocalhost%2ftext.xml )进行编码,但这也无法正常工作

The screenshot shows that the output to the webpage isn't what is expected, but that the encoded url is correct 屏幕截图显示该网页的输出不是预期的,但是编码的url是正确的

在此处输入图片说明

Your CGI script works fine for me using Apache 2.4.10 and Firefox (curl also). 使用Apache 2.4.10和Firefox(也可以使用curl),您的CGI脚本对我来说运行良好。 What web server and browser are you using? 您正在使用什么Web服务器和浏览器?

My guess is that you are using Python's CGIHTTPServer , or something based on it. 我的猜测是您正在使用Python的CGIHTTPServer或基于它的东西。 This exhibits the problem that you identify. 这表现出您发现的问题。 CGIHTTPServer assumes that it is being provided with a path to a CGI script so it collapses the path without regard to any query string that might be present. CGIHTTPServer假定为它提供了CGI脚本的路径,因此它折叠该路径而不考虑可能存在的任何查询字符串。 Collapsing the path removes duplicate forward slashes as well as relative path elements such as .. . 折叠路径会删除重复的正斜杠以及相对路径元素,例如..

If you are using this web server I don't see any obvious way around by changing the URL. 如果您使用的是此Web服务器,则更改URL不会发现任何明显的方法。 You won't be using it in production, so perhaps look at another web server such as Apache, nginx, lighttpd etc. 您将不会在生产中使用它,所以也许看看另一个Web服务器,例如Apache,nginx,lighttpd等。

The problem is with your query parameters, you should be encoding them: 问题在于查询参数,您应该对它们进行编码:

>>> 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM