Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send response code from python using cgi package

I am writing a script in python using cgi package. I need to send response of either HTTP/1.1 200 OK\n or HTTP/1.1 404 Not Found\n based on some checks in my code. If I print one of the above line as the first line of my response my apache server logs an error and returns 500 Internal server error The relevant part of the error message logged in this case is malformed header from script 'helo.py': Bad header: HTTP/1.1 200 OK\n Can anyone guide me what am I doing wrong here.

like image 908
Sharad Avatar asked Jul 25 '26 10:07

Sharad


1 Answers

Do you print only these lines? According to the CGI spec, you must define Content-Type.

print "Content-Type: text/html\r\n\r\n";

If you want to emit status, please use Status header instead (this is CGI, not HTTP header).

So

print "Status: 404 Not Found\r\n"
print "Content-Type: text/html\r\n\r\n"

This will do the trick.

like image 73
Sergey Arkhipov Avatar answered Jul 28 '26 01:07

Sergey Arkhipov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!