我目前正在开展一个学校项目,我们必须通过python脚本在cgi网站上运行一个jar文件。
我的jar输出看起来像这样:
String str = "<body>"+str+"</body>";
BufferedWriter writer = new BufferedWriter(new FileWriter(s+".html"));
writer.write(str);
writer.close();
我的python脚本是这样的:
#!/usr/bin/env python3
import cgi, cgitb
import os
import subprocess
cgitb.enable()
HTML="""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Validation</title>
</head>"""
HTML2="""
</html>
"""
out = subprocess.check_output(["java -jar test.jar])
print(out)
如果我运行它总是给我一个语法错误任何帮助非常感谢。
堆栈跟踪
CalledProcessError Python 3.7.2: C:\Users\maxgr\PycharmProjects\untitled1\venv\Scripts\python.exe
Tue Mar 5 01:26:43 2019
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
C:\Users\maxgr\PycharmProjects\untitled1\Aufgabe13\ToHtml.py in <module>()
59
60 #out = subprocess.check_output(["java -jar /home/r/reinisch/public_html/cgi-bin/secstruc/validateGor.jar -s /home/r/reinisch/public_html/cgi-bin/secstruc/seclib/" + seclib + " -f html -s /home/r/reinisch/public_html/cgi-bin/predic/" + predic +" "] ,shell=True)
=> 61 out = subprocess.check_output(["java -jar C:\\dev\\git\\out\\artifacts\\git_jar4\\git.jar -r C:\\dev\\git\\src\\secstruc\\cb513.db" + " -f html -p C:\\dev\\git\\src\\secstruc\\cb513_gor1_cb513.prd "] ,shell=True)
62
63 #out = subprocess.check_output(["echo hallo"], shell=True)
out undefined, subprocess = <module 'subprocess' from 'C:\\Users\\maxgr\\App...\Programs\\Python\\Python37\\lib\\subprocess.py'>, subprocess.check_output = <function check_output>, shell undefined
C:\Users\maxgr\AppData\Local\Programs\Python\Python37\lib\subprocess.py in check_output(timeout=None, *popenargs=([r'java -jar C:\dev\git\out\artifacts\git_jar4\git.... -p C:\dev\git\src\secstruc\cb513_gor1_cb513.prd '],), **kwargs={'shell': True})
393
394 return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
=> 395 **kwargs).stdout
396
397
kwargs = {'shell': True}, ).stdout undefined
C:\Users\maxgr\AppData\Local\Programs\Python\Python37\lib\subprocess.py in run(input=None, capture_output=False, timeout=None, check=True, *popenargs=([r'java -jar C:\dev\git\out\artifacts\git_jar4\git.... -p C:\dev\git\src\secstruc\cb513_gor1_cb513.prd '],), **kwargs={'shell': True, 'stdout': -1})
485 if check and retcode:
486 raise CalledProcessError(retcode, process.args,
=> 487 output=stdout, stderr=stderr)
488 return CompletedProcess(process.args, retcode, stdout, stderr)
489
output undefined, stdout = b'', stderr = None
CalledProcessError: Command '['java -jar C:\\dev\\git\\out\\artifacts\\git_jar4\\git.jar -r C:\\dev\\git\\src\\secstruc\\cb513.db -f html -p C:\\dev\\git\\src\\secstruc\\cb513_gor1_cb513.prd ']' returned non-zero exit status 1.
args = (1, [r'java -jar C:\dev\git\out\artifacts\git_jar4\git.... -p C:\dev\git\src\secstruc\cb513_gor1_cb513.prd '])
cmd = [r'java -jar C:\dev\git\out\artifacts\git_jar4\git.... -p C:\dev\git\src\secstruc\cb513_gor1_cb513.prd ']
output = b''
returncode = 1
stderr = None
stdout = b''
with_traceback = <built-in method with_traceback of CalledProcessError object>
Python错误:
C:\Users\maxgr\PycharmProjects\untitled1\venv\Scripts\python.exe C:/Users/maxgr/PycharmProjects/untitled1/Aufgabe13/ToHtml.py
Console output is saving to: C:\dev\git\testtohtml.html
<!--: spam
Die Syntax f�r den Dateinamen, Verzeichnisnamen oder die Datentr�gerbezeichnung ist falsch.
Content-Type: text/html
<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> -->
<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> -->
</font> </font> </font> </script> </object> </blockquote> </pre>
</table> </table> </table> </table> </table> </font> </font> </font><body bgcolor="#f0f0f8">
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#6622aa">
<td valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong>CalledProcessError</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial">Python 3.7.2: C:\Users\maxgr\PycharmProjects\untitled1\venv\Scripts\python.exe<br>Tue Mar 5 01:26:43 2019</font></td></tr></table>
<p>A problem occurred in a Python script. Here is the sequence of
function calls leading up to the error, in the order they occurred.</p>
<table width="100%" cellspacing=0 cellpadding=0 border=0>
<tr><td bgcolor="#d8bbff"><big> </big><a href="file://C:\Users\maxgr\PycharmProjects\untitled1\Aufgabe13\ToHtml.py">C:\Users\maxgr\PycharmProjects\untitled1\Aufgabe13\ToHtml.py</a> in <strong><module></strong>()</td></tr>
<tr><td><font color="#909090"><tt> <small> 59</small> <br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt> <small> 60</small> #out = subprocess.check_output(["java -jar /home/r/reinisch/public_html/cgi-bin/secstruc/validateGor.jar -s /home/r/reinisch/public_html/cgi-bin/secstruc/seclib/" + seclib + " -f html -s /home/r/reinisch/public_html/cgi-bin/predic/" + predic +" "] ,shell=True)<br>
</tt></font></td></tr>
<tr><td bgcolor="#ffccee"><tt>=><small> 61</small> out = subprocess.check_output(["java -jar C:\\dev\\git\\out\\artifacts\\git_jar4\\git.jar -r C:\\dev\\git\\src\\secstruc\\cb513.db" + " -f html -p C:\\dev\\git\\src\\secstruc\\cb513_gor1_cb513.prd "] ,shell=True)<br>
</tt></td></tr>
<tr><td><font color="#909090"><tt> <small> 62</small> <br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt> <small> 63</small> #out = subprocess.check_output(["echo hallo"], shell=True)<br>
</tt></font></td></tr>
<tr><td><small><font color="#909090">out <em>undefined</em>, <strong>subprocess</strong> = <module 'subprocess' from 'C:\\Users\\maxgr\\App...\Programs\\Python\\Python37\\lib\\subprocess.py'>, subprocess.<strong>check_output</strong> = <function check_output>, shell <em>undefined</em></font></small></td></tr></table>
<table width="100%" cellspacing=0 cellpadding=0 border=0>
<tr><td bgcolor="#d8bbff"><big> </big><a href="file://C:\Users\maxgr\AppData\Local\Programs\Python\Python37\lib\subprocess.py">C:\Users\maxgr\AppData\Local\Programs\Python\Python37\lib\subprocess.py</a> in <strong>check_output</strong>(timeout=None, *popenargs=([r'java -jar C:\dev\git\out\artifacts\git_jar4\git.... -p C:\dev\git\src\secstruc\cb513_gor1_cb513.prd '],), **kwargs={'shell': True})</td></tr>
<tr><td><font color="#909090"><tt> <small> 393</small> <br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt> <small> 394</small> return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,<br>
</tt></font></td></tr>
<tr><td bgcolor="#ffccee"><tt>=><small> 395</small> **kwargs).stdout<br>
</tt></td></tr>
<tr><td><font color="#909090"><tt> <small> 396</small> <br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt> <small> 397</small> <br>
</tt></font></td></tr>
<tr><td><small><font color="#909090"><strong>kwargs</strong> = {'shell': True}, ).stdout <em>undefined</em></font></small></td></tr></table>
<table width="100%" cellspacing=0 cellpadding=0 border=0>
<tr><td bgcolor="#d8bbff"><big> </big><a href="file://C:\Users\maxgr\AppData\Local\Programs\Python\Python37\lib\subprocess.py">C:\Users\maxgr\AppData\Local\Programs\Python\Python37\lib\subprocess.py</a> in <strong>run</strong>(input=None, capture_output=False, timeout=None, check=True, *popenargs=([r'java -jar C:\dev\git\out\artifacts\git_jar4\git.... -p C:\dev\git\src\secstruc\cb513_gor1_cb513.prd '],), **kwargs={'shell': True, 'stdout': -1})</td></tr>
<tr><td><font color="#909090"><tt> <small> 485</small> if check and retcode:<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt> <small> 486</small> raise CalledProcessError(retcode, process.args,<br>
</tt></font></td></tr>
<tr><td bgcolor="#ffccee"><tt>=><small> 487</small> output=stdout, stderr=stderr)<br>
</tt></td></tr>
<tr><td><font color="#909090"><tt> <small> 488</small> return CompletedProcess(process.args, retcode, stdout, stderr)<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt> <small> 489</small> <br>
</tt></font></td></tr>
<tr><td><small><font color="#909090">output <em>undefined</em>, <strong>stdout</strong> = b'', <strong>stderr</strong> = None</font></small></td></tr></table><p><strong>CalledProcessError</strong>: Command '['java -jar C:\\dev\\git\\out\\artifacts\\git_jar4\\git.jar -r C:\\dev\\git\\src\\secstruc\\cb513.db -f html -p C:\\dev\\git\\src\\secstruc\\cb513_gor1_cb513.prd ']' returned non-zero exit status 1.
<br><tt><small> </small> </tt>args =
(1, [r'java -jar C:\dev\git\out\artifacts\git_jar4\git.... -p C:\dev\git\src\secstruc\cb513_gor1_cb513.prd '])
<br><tt><small> </small> </tt>cmd =
[r'java -jar C:\dev\git\out\artifacts\git_jar4\git.... -p C:\dev\git\src\secstruc\cb513_gor1_cb513.prd ']
<br><tt><small> </small> </tt>output =
b''
<br><tt><small> </small> </tt>returncode =
1
<br><tt><small> </small> </tt>stderr =
None
<br><tt><small> </small> </tt>stdout =
b''
<br><tt><small> </small> </tt>with_traceback =
<built-in method with_traceback of CalledProcessError object>
<!-- The above is a description of an error in a Python program, formatted
for a Web browser because the 'cgitb' module was enabled. In case you
are not reading this in a Web browser, here is the original traceback:
Traceback (most recent call last):
File "C:/Users/maxgr/PycharmProjects/untitled1/Aufgabe13/ToHtml.py", line 61, in <module>
out = subprocess.check_output(["java -jar C:\\dev\\git\\out\\artifacts\\git_jar4\\git.jar -r C:\\dev\\git\\src\\secstruc\\cb513.db" + " -f html -p C:\\dev\\git\\src\\secstruc\\cb513_gor1_cb513.prd "] ,shell=True)
File "C:\Users\maxgr\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 395, in check_output
**kwargs).stdout
File "C:\Users\maxgr\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 487, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['java -jar C:\\dev\\git\\out\\artifacts\\git_jar4\\git.jar -r C:\\dev\\git\\src\\secstruc\\cb513.db -f html -p C:\\dev\\git\\src\\secstruc\\cb513_gor1_cb513.prd ']' returned non-zero exit status 1.
-->
进程以退出代码1结束
不应该out = subprocess.check_output(["java -jar test.jar])
:
out = subprocess.check_output(["java -jar test.jar"])
?
(缺少关闭"
)
如果这不是问题,我们需要查看完整的堆栈跟踪。