2

So i found this thread:

Python replacement for PHP's header

but i still couldn't find my answer, i don't understand the first answer because i don't use Django and i want to use pure python (I'm using CGI)

the second answer also doesn't give details, i tried using start_response() but i couldn't get it to redirect to my url, maybe i was giving it the wrong arguments

also i found another topic:

How to redirect a page to another page in python 3 cgi

but again the answer is still not as good as php header because it loads the entire page then redirects it which user can notice, the header function wasn't like this and was much faster and cleaner

i also tried webbrowser.open() and webbrowser.get().open()

but the problem is they open a new tab instead of loading it right there, even when i use the new=0 option! (not to mention some weird problem that causes it to open IE as well as chrome which we have to give full path to fix)

so if the best option is start_response(), can someone explain what exactly should i write?

what would be the equivalent of this php header:

header("location: ./index.php?msg=Invalid Username or Password");
  • You don't use Django, but what HTTP Middleware / Framework are you using ? – Blusky Nov 16 '18 at 17:04
  • @Blusky I'm actually using python just for one function and then pass the result back to my php page, i basically only use pure python and want to calculate something then send a error message or success using GET just like header, I'm also using XAMPP for to run the apache server – Richard Jones Nov 16 '18 at 17:06
  • So i have to learn Django or some other framework to achieve this or is there a simpler way? – Richard Jones Nov 16 '18 at 17:08
  • Then, if python have no handle on the HTTP middleware, you won't be able to change anything related to the HTTP Stack. – Blusky Nov 16 '18 at 17:08
  • Whatever you're using to set all of the *other* HTTP headers is what you should use to set this one. – Sammitch Nov 16 '18 at 17:10
  • @Sammitch sorry I am new to web dev, how can i know what I'm using to set other HTTP headers? I'm basically using a CGI python script and passing it an input using post method in my php page, and i just want to return a simple message to that php page to display the result, and I'm not using any framework, just using the CGI library in that python script – Richard Jones Nov 16 '18 at 17:13
  • The you need to figure out how to make that CGI library do what you want. – Sammitch Nov 16 '18 at 17:14
  • @Blusky So what should i do? should i use Django to achieve something like the php header and i cant do it with pure python? the only thing I'm using in this python is the CGI library which i use to receive input from my php page using POST method – Richard Jones Nov 16 '18 at 17:15

2 Answers2

1

To trigger a redirect, it is not enough to set the location header, you must also return the right status code, like this:

print("Status: 302")
print("Location: ./index.php?msg=Invalid Username or Password")
print()

In PHP, you don't have to explicitly set the status because the header function does that for you if you specify a Location: header.

The last print is necessary because the headers should be separated with blank line from the response body (which is empty in your case).

Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
0

The output of a CGI script should consist of two sections, separated by a blank line. The first section contains a number of headers, telling the client what kind of data is following. Python code to generate a minimal header section looks like this:

https://docs.python.org/2/library/cgi.html

print "Location: ./index.php?msg=Invalid+Username+or+Password"
print                               # blank line, end of headers
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95