gpt4 book ai didi

python - 当满足某些条件时如何执行另一个python程序?

转载 作者:太空宇宙 更新时间:2023-11-03 17:40:07 24 4
gpt4 key购买 nike

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')

网络服务器正在后台运行。如果上面代码中的条件为真,我想要网页重定向到另一个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>&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>

当我单击提交按钮时,validate.py 运行。现在,如果用户名和密码匹配,我希望 Web 浏览器自动将浏览器重定向到另一个 py 文件或 html 页面,而不需要用户执行任何操作。

编辑2:

我正在使用一个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”在网络浏览器中打开索引页面。

最佳答案

使用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)

关于python - 当满足某些条件时如何执行另一个python程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30673475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com