Came across a strange issue.
I am passing a dictionary as part of a POST request to a cgi script:
self.settings = {
'SubmitCommands': ['C:\Python27\python.exe path\my_script.py']
}
Here is my code:
settings = self.settings
# Make web call to CGI script
user_agent = r'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = {'User-Agent' : user_agent, "Accept": "text/plain"}
response = requests.post(
"path\CGI\cgi_script.py",
headers=headers, data=settings)
And here is the resulting traceback:
Traceback (most recent call last):
File "path\CGI\cgi_script.py", line 119, in initi
alizeJob
self.submitCommands = eval(inputSubmitCommands)
File "<string>", line 1
C:\Python27\python.exe path\my_script.py
^
SyntaxError: invalid syntax
However, if I do this the old fashioned way I do not get the traceback:
settings = self.settings
# Set and encode parameters
params = urllib.urlencode(settings)
# Make web call to CGI script
user_agent = r'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = {'User-Agent' : user_agent,
"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = httplib.HTTPConnection(self.host)
conn.request("POST", "path/CGI/cgi_script.py", params, headers)
response = conn.getresponse()
Earlier today someone warned me on Stack Overflow for picking a library just because it is new. Looks like I should have listened to them. However, I am still curious about what went wrong.
Python is interpreting your backslashes as escape codes. Just double them up to fix that.
self.settings = {
'SubmitCommands': ['C:\\Python27\\python.exe path\\my_script.py']
}
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.