【问题标题】:Infinite loading php page while opening a python file打开python文件时无限加载php页面
【发布时间】:2020-08-29 15:45:44
【问题描述】:

我对在 PHP 站点中包含 Python 还是很陌生。我正在创建一个 Python 闹钟,女巫应该打开位于我的 PC 上的不同网页。我有一个 PHP 页面,它会在启动时自动打开,我可以在其中花时间醒来。当我按“保存”时,它应该启动 python 文件,然后应该打开另一个页面并启动时钟。我现在的问题是起始页面无限加载并且没有任何反应。我做错了什么。

我的代码如下:

index.php:

<?php
if(isset($_POST['submit'])){
  $Name = "".$_POST['username']."
  ";
  $Pass = "".$_POST['password']."
  ";
  $file=fopen("saved.txt", "a");
  fwrite($file, $Name);
  fwrite($file, $Pass);
  fclose($file);

$command = escapeshellcmd('python /home/lul/Desktop/wecker.py');
   $output = shell_exec($command);
   echo $output;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="styles.css">
  <title>My HTML Form</title>
</head>
<body class="blogdesire-body">
  <div class="blogdesire-wrapper">
    <div class="blogdesire-heading">
      My HTML Form
    </div>
    <form class="blogdesire-form" method="post">
      <input type="number" name="username" placeholder="Hour" required autocomplete="off"> <br>
      <input type="number" name="password" placeholder="Minutes" required autocomplete="off"> <br>
      <input type="submit" name="submit" value="SAVE" class="blogdesire-submit">
    </form>
  </div>
</body>
</html>

python 代码:

#!/usr/bin/python

import webbrowser
import datetime
import os

print('Try opening file')

fileHandler = open ("/var/www/html/saved.txt")

listOfLines = fileHandler.readlines()

fileHandler.close()

hours = int(listOfLines[0])

minutes = int(listOfLines[1])

print(hours, minutes)
print(datetime.datetime.now().hour, datetime.datetime.now().minute)

os.remove("/var/www/html/saved.txt")

webbrowser.open("file:///home/lul/Desktop/startup/clock.html")

print("Done Opening")

while (1 == 1):
    if (hours == datetime.datetime.now().hour and minutes == datetime.datetime.now().minute):
        webbrowser.open("file:///home/lul/Desktop/morning/morning.html")

print('Sup')

【问题讨论】:

    标签: python php html css


    【解决方案1】:

    也许你错过了,但你有一个

    while(1==1)
    

    在 python 脚本中。这将无限执行接下来的 2 行

    【讨论】:

    • 是的,但我想在启动 while 循环之前打开另一个页面。使用第一个 Webbrowser.open,这永远不会发生。
    • 不,它没有。但是在测试中我从 python 文件中得到了一些错误,所以我认为它实际上是在打开 .py 文件
    • 错误已修复。 Mow 我没有收到任何错误,但也没有任何反应
    • 可悲的是,当我自己在浏览器中输入路径时,它会正常打开
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    相关资源
    最近更新 更多