如何满足一些条件时执行另一个Python程序? [英] How to execute another python program when some condition is met?
问题描述
这是validate.py
导入cgi
导入yate
导入sqlite3
connection = sqlite3.connect('users.sqlite')
cursor = connection.cursor()
print(yate.start_response('text / plain'))
form = cgi.FieldStorage()
for form_keys()中的each_form_item:
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用于用户名中的每个用户名的
:
if(username == each_username):
pass_result = cursor.execute('SELECT PASSWORD from validate where username =''(each_username,))
password1 = [row [0] for pass_result.fetchall()]
for password1中的each_password:
if(each_password == password):
print('登录成功')
#希望运行另一个python程序,例如welcome.py
else:
print('Login Failure')
Web服务器正在后台运行。如果上述代码中的条件为真,我想要网页重定向到另一个python程序。我怎么做?
编辑:
Index.html
< HTML>
< head>
< title>登入网页< / title>
< link type =text / css =stylesheethref =coach.css/>
< / head>
< body>
< img src =images / logo-cel-transparent_0.pngwidth =74height =64> < strong>< img src =images / logo-cel-transparent_0.pngalt =Cel logowidth =74height =64align =right>
< / strong>
< h1 align =center>< strong> Central Electronics Limited< / strong>< / h1>
< p>& nbsp;< / p>
< h2 align =center>存储管理系统< / h2>
< p>& nbsp;< / p>
< p align =center>登入系统< / p>
< p align =center>& nbsp;< / p>
< form action =/ cgi-bin / validate.pymethod =post>
< div align =center>用户名:
< input type =textname =usernamevalue => <峰; br>
密码:< input type =textname =passwordvalue => <峰; br>
< input name =Submittype =submitvalue =Submit>
< / div>
< / form>
< / body>
< / html>
当我点击提交按钮validate.py运行。现在,如果用户名和密码匹配,我希望网络浏览器自动将浏览器重定向到另一个py文件或html页面,而无需用户执行任何操作。
编辑2 :
我使用的是一个python服务器,其代码为:
从http.server导入HTTPServer,CGIHTTPRequestHandler
port = 8080
httpd = HTTPServer(('',port),CGIHTTPRequestHandler)
print(Starting simple_httpd on port:+ str(httpd.server_port))
httpd.serve_forever()
使用命令提示符运行。然后使用localhost:8080在Web浏览器中打开索引页。 使用 execfile
命令:
用于用户名中的每个用户名:
if(username == each_username):
pass_result = cursor.execute('SELECT PASSWORD from validate where username =?',(each_username,))
password1 = [row [0]用于pass_result.fetchall()中的行)
for password中的each_password:
if(each_password == password):
print('Login Success')
execfile(PATH_TO_THE_FILE +./ welcome.py)
else:
print('登录失败')
注意: PATH_TO_THE_FILE
是您的磁盘中的路径,它应该是一个字符串
编辑
你是 使用python 3x,这里是 execfile
命令的替代:
f:
code = compile(f.read(),welcome.py,'exec')
exec(代码)带有开放(welcome.py
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> </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>
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".
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)
这篇关于如何满足一些条件时执行另一个Python程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!