【问题标题】:How to execute another python program when some condition is met?满足某些条件时如何执行另一个python程序?
【发布时间】: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>&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 运行。现在,如果用户名和密码匹配,我希望网络浏览器自动将浏览器重定向到另一个 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 页面,我将其代码发布在下面。
  • 我在编辑中发布了,请看上面:)

标签: python html


【解决方案1】:

以这种方式使用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)

【讨论】:

  • 它给出了未定义的 NameError execfile。可能是因为我使用的是 Python 3.4?
  • 顺便说一句,是否可以对 html 页面使用 execfile 命令?
  • @ShivamAgarwal 我不确定,但我不这么认为
【解决方案2】:

你试过 Python subprocesses?

【讨论】:

  • 是的,但它显示错误消息:访问被拒绝
  • \r\n subprocess.call(\'C:\\Python34\\ProjectShivam\\webapp\\cgi-bin\')\r \n 文件“C:\\Python34\\ lib\\subprocess.py",第 537 行,调用\r\n,Popen (*popenargs, **kwargs) 作为 p:\r\n 文件 "C:\\Python34\\lib\\subprocess.py" ,第 859 行,在 __init__\r\n restore_signals,start_new_session)\r\n 文件“C:\\Pytho n34\\lib\\subprocess.py”,第 1112 行,在 _execute_child\r\n startupinfo)\r\ nPermissionError: [WinError 5] 访问被拒绝\r\n' 127.0.0.1 - - [06/Jun/2015 00:16:00] CGI 脚本退出状态 0x1
  • 您尝试执行的脚本在哪里?尝试以管理员身份运行父进程。
  • 对不起,先生,这超出了我的想象
【解决方案3】:

您可以在其他 Python 文件中创建一个将页面作为参数的方法,而不是生成子进程。然后,您可以将该方法导入您发布的文件中。

在你要调用的文件中“file.py”:

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

【讨论】:

    【解决方案4】:

    因此,您似乎想从 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')
    

    【讨论】:

    • 我这样做了,但它在浏览器中以纯文本形式打印了位置。它没有重定向它!另请参阅问题中的编辑,我使用的是 python 内置服务器。
    • 你确定你没有在之前的标题之后发送一个空行吗?
    • 是的!该位置是整个 validation.py 程序中的第一个打印语句。
    • 我编辑了我的答案,我可能弄错了。请尝试现在的代码。附带说明一下,您应该考虑使用像 django 这样的 Web 框架。这将使事情变得容易得多!
    • 它显示一个空白页面,并且 url 没有改变。但是感谢您的帮助!我会调查django。希望这不会超出我的想象。 :)
    猜你喜欢
    • 1970-01-01
    • 2018-01-09
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多