首页 > 解决方案 > 即使脚本有效,也会收到“标题前脚本输出结束”错误

问题描述

我创建了一个简单的 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

标签: pythonhtmlxampp

解决方案


正如@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>')

推荐阅读