Ok, so I have a website, and I'm making a python script to insert data into the website via sending it to a php script as a GET request, but whenever I put a script with not alphabetic or numeric characters ex(@[];:) I get a urllib error saying I don't have a host in the 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
I also tried making my own escape function to escape all the special chars (or at least a few)
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
But still, nothing. Please suggest on how to fix this problem! Thanks in advance.
One problem might be that cgi.escape
doesn't do what you think it does; look at 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.
This behaves a little more sensibly in general:
>>> 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'
>>>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.