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')
Web服务器在后台运行。如果上面代码中的条件为真,我想将网页重定向到另一个python程序。我怎么做?
编辑:
的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> </p>
<h2 align="center">Storage Management System</h2>
<p> </p>
<p align="center">Login to System</p>
<p align="center"> </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>
当我点击提交按钮时,validate.py会运行。现在,如果用户名和密码匹配,我希望Web浏览器自动将浏览器重定向到另一个py文件或html页面,而无需用户做任何事情。
EDIT2:
我正在使用代码为:
的python服务器 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()
这是使用命令提示符运行的。然后使用&#34; localhost:8080&#34;。
在Web浏览器中打开索引页面答案 0 :(得分:1)
您是否尝试过Python subprocesses?
答案 1 :(得分:0)
以这种方式使用execfile
命令:
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')
注意:PATH_TO_THE_FILE
是磁盘中的路径,它应该是一个字符串
修改强>
当您使用python 3x时,这里是execfile
命令的替代方法:
with open("welcome.py") as f:
code = compile(f.read(), "welcome.py", 'exec')
exec(code)
答案 2 :(得分:0)
您可以在其他Python文件中创建一个以页面为参数的方法,而不是生成子流程。然后,您可以将该方法导入到已发布的文件中。
在您要调用的文件中&#34; file.py&#34;:
def start(page):
do_something()
现在:
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')
答案 3 :(得分:0)
因此,您似乎希望从CGI脚本内部重定向用户以处理POST请求。好吧,为此,请设置Location
标题。您似乎正在使用库来打印所有标题,但您需要更改它。
正如here所示,这看起来像这样:
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')