【发布时间】:2015-06-05 18:32:35
【问题描述】:
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 程序。我怎么做?
编辑:
索引.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 运行。现在,如果用户名和密码匹配,我希望网络浏览器自动将浏览器重定向到另一个 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()
这是使用命令提示符运行的。然后我使用“localhost:8080”在网络浏览器中打开索引页面。
【问题讨论】:
-
我怀疑你并不是真的想产生一个全新的 Python 进程,但也许只是产生一个新线程。
-
我不知道你说了什么,但基本上我只是想将页面重定向到新的 html 页面或 python 程序。这两种可能吗?
-
重定向谁?这个跑得怎么样?用户如何访问它?这是完整的代码吗?
-
感谢您抽出宝贵时间提供帮助。我会详细说。不,这不是完整的代码。最初,我们有一个名为 index.html 的 html 页面,我将其代码发布在下面。
-
我在编辑中发布了,请看上面:)