簡體   English   中英

從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是正確的

在此處輸入圖片說明

使用Apache 2.4.10和Firefox(也可以使用curl),您的CGI腳本對我來說運行良好。 您正在使用什么Web服務器和瀏覽器?

我的猜測是您正在使用Python的CGIHTTPServer或基於它的東西。 這表現出您發現的問題。 CGIHTTPServer假定為它提供了CGI腳本的路徑,因此它折疊該路徑而不考慮可能存在的任何查詢字符串。 折疊路徑會刪除重復的正斜杠以及相對路徑元素,例如..

如果您使用的是此Web服務器,則更改URL不會發現任何明顯的方法。 您將不會在生產中使用它,所以也許看看另一個Web服務器,例如Apache,nginx,lighttpd等。

問題在於查詢參數,您應該對它們進行編碼:

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