好的,所以我有一个网站,我正在制作一个 python 脚本,通过将数据作为 GET 请求发送到 php 脚本来将数据插入网站,但是每当我放置一个没有字母或数字字符的脚本 ex(@[ ];:) 我收到一个 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
但是,还是什么都没有。请建议如何解决此问题!提前致谢。