0

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.

3
  • Is the parameter to requests.post and conn.request actually a path? Or is it a URL? Commented Oct 16, 2014 at 2:50
  • The parameter is a path. Commented Oct 16, 2014 at 16:49
  • That's very odd. On my machine, when I try using a path (requests.post("/tmp/CGI/cgi_script.py")), I get MissingSchema: Invalid URL u'/tmp/CGI/cgi_script.py': No schema supplied. Commented Oct 16, 2014 at 17:08

1 Answer 1

2

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']
}
Sign up to request clarification or add additional context in comments.

1 Comment

or use a raw string r'C:\Python27...'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.