Jump to content

page redirection in python

- - - - -

  • Please log in to reply
11 replies to this topic

#1
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
In a Python script running under CGI, can I programatically redirect the
program to another page. Assume that I have a static HTML page that I want
displayed (e.g. index.htm). Other than 'print ...' is there any way to
redirect to this URL (for example, like Response.Redirect() in ASP)?

thanks

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I don't know Python, but I had to deal with HTTP in the past. If you haven't sent any HTML, yet, to the client, then you can redirect the browser with a response header, 'location' .

Like in Perl, (if you're still in the header part, that is; if you haven't sent any HTML yet, nor closed the header) you would say:
print "Location: redirect_to_page.html\r\n"; 


#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
Python has a ton of modules and frameworks out there, so it's possible to find one that might do something that you want, but I don't know of any. I would recommend django framework, as it's super easy and fun to work with.

Or you could do as RhetoricalRuvim said, and just print the redirect.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
thanks for the reply but it does not work, what i want is to direct a user to another web page if her login account is okay eg

if username and password == okay:

 direct to success_page.py


else:

direct to errorpage.py


IN PHP its done this way

header("Location: success.php");


Any help on that

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

mutago said:

if username and password == okay:

What do you mean by that? Don't you mean this?:
if username == okay and password == okay: 

Even if Python does have weird syntax, I don't think it's that weird.

* * *

mutago said:

header("Location: success.php");

In Perl, you can do it like this:
print "Location: success.php\r\n"; 

Not very different, don't you think? I'm sure it's something similar in Python.

#6
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
thanks but it still not work that way. success.html is a page not words message, cannot be printed. any further suggestion will be appreciated
thanks

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Does it redirect at all? Can you provide more details?

#8
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
it returns an error page if i want direct a web page as you said. I think that we are trying to
print a webpage using this

print "Location: success.php\r\n";

location cannot be printed because its not word message
okay consider php as a case study which also use properties print or echo
but to direct a page in php it becomes

header("Location: success.php");

i think the problem lies in the fact that a web page cannot be printed as we are trying to do
thanks
still counting on you

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
Try this
def redirect(url):
    print "Content-Type: text/plain"
    print "Refresh: 0; url=%s" % url
    print
    print "Redirecting..."
Source.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#10
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
@mutago: Are you print'ing the header before anything else on the page? Also, are you closing the header by printing an extra "\r\n" sequence?

@Flying Dutchman: I thought the print keyword did not append new lines to the end of the string it's outputting; doesn't only puts () do that?

#11
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
In Python 2.x print does append new line, unless you end it with comma.
print "Line 1"
print "Line 2",
print "Line 3"

# output
Line 1
Line 2 Line 3

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#12
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
thanks you are correct




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users