1
This is validate.py

import cgi
import yate
import sqlite3


connection = sqlite3.connect('users.sqlite')
cursor = connection.cursor()

print(yate.start_response('text/plain'))
form=cgi.FieldStorage()
for each_form_item in form.keys():
  if (each_form_item=='username'):
    username=form[each_form_item].value
  if (each_form_item=='password'):
    password=form[each_form_item].value

result=cursor.execute('SELECT USERNAME from validate')
usernames=[row[0] for row in result.fetchall()]
for each_username in usernames:
    if (username==each_username):
        pass_result=cursor.execute('SELECT PASSWORD from validate where username=?',(each_username,))
    password1=[row[0] for row in pass_result.fetchall()]
    for each_password in password1:
        if (each_password==password):
            print('Login Success')
           #Here want to run another python program eg welcome.py
        else:
            print('Login Failure')

The web server is running in the background. I want to webpage to redirect to another python program if the condition in the above code is true. How do I do that?

EDIT:

Index.html

<html>
<head>
 <title>Login Page</title>
 <link type="text/css" rel="stylesheet" href="coach.css" />
  </head>
  <body>
  <img src="images/logo-cel-transparent_0.png" width="74" height="64">      <strong><img src="images/logo-cel-transparent_0.png"  alt="Cel logo" width="74" height="64" align="right">
     </strong>
     <h1 align="center"><strong>Central Electronics Limited</strong></h1>
    <p>&nbsp;</p>
      <h2 align="center">Storage Management System</h2>
    <p>&nbsp;</p>
       <p align="center">Login to System</p>
     <p align="center">&nbsp;</p>
     <form action="/cgi-bin/validate.py" method="post">
    <div align="center">User name :
     <input type="text" name="username" value=""> <br>
       Password : <input type="text" name="password" value="">  <br>
     <input name="Submit" type="submit" value="Submit">
     </div>
     </form>
     </body>
     </html>

When i click on the submit button validate.py runs. Now if the username and password match, I want the web browser to automatically redirect the browser to another py file or html page, without the user needing to do anything.

EDIT2:

I am using a python server whose code is:

  from http.server import HTTPServer, CGIHTTPRequestHandler

  port = 8080

   httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
   print("Starting simple_httpd on port: " + str(httpd.server_port))
   httpd.serve_forever()

This is run using command prompt. The index page i then open in web browser using "localhost:8080".

5
  • I suspect that you don't really want to spawn an entirely new Python process, but maybe just spawn a new thread. Commented Jun 5, 2015 at 18:42
  • I have no idea what you said, but basically i just want to redirect the page to a new html page or a python program. Is any of the two possible? Commented Jun 5, 2015 at 18:54
  • Redirect who? How is this run? And how is the user accessing it? Is that the complete code? Commented Jun 5, 2015 at 18:57
  • Thanks for taking the time and helping out. I'll tell in detail. No that is not the complete code. Initially, we have an html page called index.html whose code i am posting below. Commented Jun 5, 2015 at 19:00
  • I posted that in the edit, please see above :) Commented Jun 5, 2015 at 19:07

4 Answers 4

1

Use the execfile command this way:

for each_username in usernames:
    if (username==each_username):
        pass_result=cursor.execute('SELECT PASSWORD from validate where username=?',(each_username,))
    password1=[row[0] for row in pass_result.fetchall()]
    for each_password in password1:
        if (each_password==password):
            print('Login Success')
            execfile(PATH_TO_THE_FILE+"./welcome.py")
        else:
            print('Login Failure')

Note: PATH_TO_THE_FILE is the path in your disk and it should be a string

EDIT

As you are using python 3x, here is the alternative of execfile command:

with open("welcome.py") as f:
    code = compile(f.read(), "welcome.py", 'exec')
    exec(code)
Sign up to request clarification or add additional context in comments.

3 Comments

It gives NameError execfile not defined. Probably becuase i am using Python 3.4?
Just on a side note, is it possible to use the execfile command for an html page?
@ShivamAgarwal I am not sure but I don't think so
1

Have you tried Python subprocesses?

4 Comments

Yes i have but it displays an error message: access is denied
\r\n subprocess.call(\'C:\\Python34\\ProjectShivam\\webapp\\cgi-bin\')\r \n File "C:\\Python34\\lib\\subprocess.py", line 537, in call\r\n with Popen (*popenargs, **kwargs) as p:\r\n File "C:\\Python34\\lib\\subprocess.py", line 859, in __init__\r\n restore_signals, start_new_session)\r\n File "C:\\Pytho n34\\lib\\subprocess.py", line 1112, in _execute_child\r\n startupinfo)\r\nPe rmissionError: [WinError 5] Access is denied\r\n' 127.0.0.1 - - [06/Jun/2015 00:16:00] CGI script exit status 0x1
Where is the script you're trying to execute located? Try running the parent process as an administrator.
Sorry sir but that went over my head
0

Instead of spawning a subprocess, you could create a method in your other Python file that takes a page as an argument. You could then import that method into the file you have posted.

In the file you want to call "file.py":

def start(page):
    do_something()

And now:

from file import start

for each_username in usernames:
    if (username==each_username):
        pass_result=cursor.execute('SELECT PASSWORD from validate where username=?',(each_username,))
password1=[row[0] for row in pass_result.fetchall()]
for each_password in password1:
    if (each_password==password):
        print('Login Success')
       start(some_page)
    else:
        print('Login Failure')

Comments

0

So it appears you want to redirect the user from inside a CGI script to process a POST request. Well, to do that, set the Location header. It appears you are using a library to print all headers, but you will need to change that.

As demonstrated here, this can look something like this:

import cgi
import yate
import sqlite3


connection = sqlite3.connect('users.sqlite')
cursor = connection.cursor()



form=cgi.FieldStorage()
for each_form_item in form.keys():
  if (each_form_item=='username'):
    username=form[each_form_item].value
  if (each_form_item=='password'):
    password=form[each_form_item].value

result=cursor.execute('SELECT USERNAME from validate')
usernames=[row[0] for row in result.fetchall()]
for each_username in usernames:
    if (username==each_username):
        pass_result=cursor.execute('SELECT PASSWORD from validate where username=?',(each_username,))
    password1=[row[0] for row in pass_result.fetchall()]
    for each_password in password1:
        if (each_password==password):
            print('Location:your/new/url')
        else:
            print('Content-type:text/plain')
            print('')
            print('Login Failure')

5 Comments

I did that, but it printed the Location in plain text in the browser. It did not redirect it! Also please see the edit in the question, I am using a python inbuilt server.
Did you make sure you had not already sent a blank line after previous headers?
Yes! The location was the first print statement in the whole validation.py program.
I edited my answer, I may have mistaken. Please try the code as it is now. On a side note, you should look into using a web framework like django. That will make things a lot easier!
It shows a blank page and the url doesnt change. But thanks for helping! And i'll look into django. Hope that doesn't go over my head. :)

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.