为什么我的网址访问失败


Why does my url access fail?

好的,所以我有一个网站,我正在制作一个python脚本,通过将其作为GET请求发送到php脚本来将数据插入网站,但是每当我放置一个没有字母或数字字符的脚本时,例如(@[];:)我收到一个 urllib 错误,指出我的 url 中没有主机:

        return urllib.urlopen("http://this-is-an-example.com/thisisadirectory/file.php?f=Hello&v="+cgi.escape("This is@A#!T33ST::::;'[]{}"))
      File "Python25'lib'urllib.py", line 82, in urlopen
        return opener.open(url)
      File "Python25'lib'urllib.py", line 190, in open
        return getattr(self, name)(url)
      File "Python25'lib'urllib.py", line 301, in open_http
        if not host: raise IOError, ('http error', 'no host given')
    IOError: [Errno http error] no host given

我还尝试制作自己的转义函数来转义所有特殊字符(或至少几个)

    full_escape_chars = {" ": "%20",
                    "<": "%3C",
                    ">": "%3E",
                    "#": "%23",
                    "'%": "%25",
                    "{": "%7B",
                    "}": "%7D",
                    "|": "%7C",
                    "''": "%5C",
                    "^": "%5E",
                    "~": "%7E",
                    "[": "%5B",
                    "]": "%5D",
                    "`": "%60",
                    ";": "%3B",
                    "/": "%2F",
                    "?": "%3F",
                    ":": "%3A",
                    "@": "%40",
                    "=": "%3D",
                    "&": "%26",
                    "$": "%24"}
    def full_escape(s):
        global full_escape_chars
        for key in full_escape_chars.keys():
            s = s.replace(key, full_escape_chars[key])
        return s

但是,仍然没有。请就如何解决此问题提出建议!提前谢谢。

一个问题可能是cgi.escape没有做你认为它做的事情; 看看urllib.quote_plus

>>> import cgi
>>> import urllib
>>> s = "This is@A#!T33ST::::;'[]{}"
>>> cgi.escape(s)
"This is@A#!T33ST::::;'[]{}"
>>> urllib.quote_plus(s)
'This+is%40A%23%21T33ST%3A%3A%3A%3A%3B%27%5B%5D%7B%7D'

cgi.escape(s[, quote])

Convert the characters '&', '<' and '>' in string s to HTML-safe sequences. 
Use this if you need to display text that might containsuch characters in HTML.

一般来说,这表现得更明智一些:

>>> urllib.urlopen("http://this-is-an-example.com/thisisadirectory/file.php?f=Hello&v="+urllib.quote_plus("Thi
s is@A#!T33ST::::;'[]{}"))
<addinfourl at 24629656 whose fp = <socket._fileobject object at 0x16ebc30>>
>>> _.read()
'<?xml version="1.0" encoding="iso-8859-1"?>'n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'n
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'n<html xmlns="http://www.w3.org/1999/xhtml
" xml:lang="en" lang="en">'n <head>'n  <title>404 - Not Found</title>'n </head>'n <body>'n  <h1>404 - Not Foun
d</h1>'n </body>'n</html>'n'
>>>