2

I am writing a Python script to pre-process SVG files. I call a URL like:

http://example.com/includesvg.py?/myfile.svg

I would like the script "includesvg.py" to return a 404 error if myfile.svg does not exist. I have tried:

print "Status: 404 Not Found\r\n"
print "Content-Type: text/html\r\n\r\n"
print "<h1>404 File not found!</h1>"

It doesn't work.

This must be a duplicate question, but there are so many questions with "Python" and "404" that I can't find an existing answer.

linusg
  • 6,289
  • 4
  • 28
  • 78
Andy Swift
  • 2,179
  • 3
  • 32
  • 53
  • Ehm, this is not even a valid HTTP header. There's no `Status` field, returning the status works different. – linusg Jan 25 '17 at 13:10
  • I know it's not a valid HTTP header, that's why I'm asking the question. – Andy Swift Jan 25 '17 at 13:15
  • Yeah, I get it. See my answer below. In any case, there's a Wikipedia artice for all valid HTTP header fields out there, you may add some more. – linusg Jan 25 '17 at 13:17

1 Answers1

0

The HTTP response you send is invalid. I would go with this:

print('HTTP/1.1 404 Not Found\r\n')
print('Content-Type: text/html\r\n\r\n')
print('<html><head></head><body><h1>404 Not Found</h1></body></html>')

Resulting in (prettified):

HTTP/1.1 404 Not Found
Content-Type: text/html

<html>
  <head>
  </head>
  <body>
    <h1>404 Not Found</h1>
  </body>
</html>

However, this one would also work:

import sys
sys.stdout('Status: 404 Not Found\r\n\r\n')

As a simplified version of:

print('Status: 404 Not Found')
print()

Hope this helps!

linusg
  • 6,289
  • 4
  • 28
  • 78
  • If I print "\n" first, I just get text with a 200 status (I checked at http://www.rexswain.com/cgi-bin/httpview.cgi). If I leave out the "\n" I get a server error. – Andy Swift Jan 25 '17 at 13:21
  • Ok, I've edited. I guess it's an issue with the builtin `print` adding newlines as it wants. – linusg Jan 25 '17 at 13:24
  • 1
    Same problem... if I don't include the newline before I get 500 server error, and if I do, the status is automatically set to 200. Any effort to print something without a preceding newline causes a server error. I'm going to look in my logs what the exact error is. – Andy Swift Jan 25 '17 at 13:31
  • OK, please comment on any errors you find. However, the HTTP response should be built like above, thats the standard for years. If it doesn't help either, try creating a file with a correct HTTP response with an editor like Notepad++ (for correc tline endings) and read and print it from Python. – linusg Jan 25 '17 at 13:34
  • I don't think the problem is with the construction of the text. I think what I'm missing is an understanding of how Python outputs headers vs how Python outputs text. There must be some function or library that I need to output a header besides just "print". Anyway, thanks for your help. – Andy Swift Jan 25 '17 at 13:40
  • No, that's the joke. In Python CGI, `print`ing text will create the response. Think of it like a temp text file, `print` will add text to it and once the script is finished executing, the content of this 'file' is given as HTTP response to the client. – linusg Jan 25 '17 at 13:42
  • What worked is print 'Status: 404 Not Found' on one line then just "print" on the next line (http://stackoverflow.com/questions/1411867/python-cgi-returning-an-http-status-code-such-as-403) If you want to write up an answer I'll select it as true. – Andy Swift Jan 25 '17 at 13:59