python - 即使脚本有效,也会收到“标题前脚本输出结束”错误
问题描述
我创建了一个简单的 html 页面来发布到 python 脚本
<!DOCTYPE html>
<html>
<body>
<h1>Name</h1>
<form action="/cgi-bin/ver1.py" method="get">
<label for="fname">VIN</label>
<input type="text" id="fname" name="searchbox"><br><br>
<button type="submit" formtarget="_blank">Submit to a new window/tab</button>
</form>
</body>
</html>
ver1.py 看起来像这样:
#!C:\Python\python.exe
import requests
import webbrowser
import cgi
form = cgi.FieldStorage()
vin=form.getvalue('searchbox')
token='withheld'
dr=requests.get("withheld" % (token, vin))
window_stkr=(dr.json()["car"]["sticker"]["pdf"])
webbrowser.open_new_tab(window_stkr)
一切正常,除了使用 window_stkr 打开一个新选项卡的部分之外,它还使用
头文件前的脚本输出结束
错误
从 apache 查看错误日志时,唯一的一行如下:
[Fri Sep 11 00:46:21.231868 2020] [cgi:error] [pid 11624:tid 1896] [client ::1:54144] 头文件前的脚本输出结束:ver1.py,引用者:http://localhost/解码器.html
解决方案
正如@furas 指出的那样,这是因为我没有向浏览器发送任何响应。
我进行了以下更改,而不是 3 个选项卡,我现在有一个 html 页面,并且 pdf 在曾经出现错误的窗口中打开:
#!C:\Python\python.exe
import requests
import webbrowser
import cgi
form = cgi.FieldStorage()
vin=form.getvalue('searchbox')
token='withheld'
dr=requests.get("withheld" % (token, vin))
window_stkr=(dr.json()["car"]["sticker"]["pdf"])
#webbrowser.open_new_tab(window_stkr)
print('Content-type:text/html\n\n')
print('<html>')
print(' <head>')
print(' <meta http-equiv="refresh" content="0;url='+str(window_stkr)+'" />')
print(' </head>')
print('</html>')
推荐阅读
- clang-format - 指定保存在 $HOME 中的 clang 格式文件
- c# - 消息:Assert.AreEqual 失败。预期:<102.01>。实际:<102.01>
- css - 如何自定义 Toast 窗口的样式?
- java - 将从文件读取的字符串转换为 JSONObject android
- c# - 如何获取另一个 C# 应用程序读取的内存地址?
- ios - “打开方式”应该在 iPhone 的相机胶卷中显示我的应用程序
- java - 单元测试的测试用例成功,但使用 NoSuchMethodError 的仪器测试失败
- kotlin - 错误:不能使用提供的参数调用以下函数
- android - 将 Background 属性添加到自定义 TextView 组件,底部只有边框
- java - 如何增加底栏 MPAndroidChart 的字体大小