我如何通过python在cgi网站上执行jar?

问题描述 投票:0回答:1

我目前正在开展一个学校项目,我们必须通过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>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<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>&nbsp;</big><a href="file://C:\Users\maxgr\PycharmProjects\untitled1\Aufgabe13\ToHtml.py">C:\Users\maxgr\PycharmProjects\untitled1\Aufgabe13\ToHtml.py</a> in <strong>&lt;module&gt;</strong>()</td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;59</small>&nbsp;<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;60</small>&nbsp;#out&nbsp;=&nbsp;subprocess.check_output(["java&nbsp;-jar&nbsp;/home/r/reinisch/public_html/cgi-bin/secstruc/validateGor.jar&nbsp;-s&nbsp;/home/r/reinisch/public_html/cgi-bin/secstruc/seclib/"&nbsp;+&nbsp;seclib&nbsp;+&nbsp;"&nbsp;-f&nbsp;html&nbsp;-s&nbsp;/home/r/reinisch/public_html/cgi-bin/predic/"&nbsp;+&nbsp;predic&nbsp;+"&nbsp;"]&nbsp;,shell=True)<br>
</tt></font></td></tr>
<tr><td bgcolor="#ffccee"><tt>=&gt;<small>&nbsp;&nbsp;&nbsp;61</small>&nbsp;out&nbsp;=&nbsp;subprocess.check_output(["java&nbsp;-jar&nbsp;C:\\dev\\git\\out\\artifacts\\git_jar4\\git.jar&nbsp;-r&nbsp;C:\\dev\\git\\src\\secstruc\\cb513.db"&nbsp;+&nbsp;"&nbsp;-f&nbsp;html&nbsp;-p&nbsp;C:\\dev\\git\\src\\secstruc\\cb513_gor1_cb513.prd&nbsp;"]&nbsp;,shell=True)<br>
</tt></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;62</small>&nbsp;<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;63</small>&nbsp;#out&nbsp;=&nbsp;subprocess.check_output(["echo&nbsp;hallo"],&nbsp;shell=True)<br>
</tt></font></td></tr>
<tr><td><small><font color="#909090">out <em>undefined</em>, <strong>subprocess</strong>&nbsp;= &lt;module 'subprocess' from 'C:\\Users\\maxgr\\App...\Programs\\Python\\Python37\\lib\\subprocess.py'&gt;, subprocess.<strong>check_output</strong>&nbsp;= &lt;function check_output&gt;, shell <em>undefined</em></font></small></td></tr></table>
<table width="100%" cellspacing=0 cellpadding=0 border=0>
<tr><td bgcolor="#d8bbff"><big>&nbsp;</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>&nbsp;&nbsp;<small>&nbsp;&nbsp;393</small>&nbsp;<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;394</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;run(*popenargs,&nbsp;stdout=PIPE,&nbsp;timeout=timeout,&nbsp;check=True,<br>
</tt></font></td></tr>
<tr><td bgcolor="#ffccee"><tt>=&gt;<small>&nbsp;&nbsp;395</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;**kwargs).stdout<br>
</tt></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;396</small>&nbsp;<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;397</small>&nbsp;<br>
</tt></font></td></tr>
<tr><td><small><font color="#909090"><strong>kwargs</strong>&nbsp;= {'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>&nbsp;</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>&nbsp;&nbsp;<small>&nbsp;&nbsp;485</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;check&nbsp;and&nbsp;retcode:<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;486</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise&nbsp;CalledProcessError(retcode,&nbsp;process.args,<br>
</tt></font></td></tr>
<tr><td bgcolor="#ffccee"><tt>=&gt;<small>&nbsp;&nbsp;487</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output=stdout,&nbsp;stderr=stderr)<br>
</tt></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;488</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;CompletedProcess(process.args,&nbsp;retcode,&nbsp;stdout,&nbsp;stderr)<br>
</tt></font></td></tr>
<tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;489</small>&nbsp;<br>
</tt></font></td></tr>
<tr><td><small><font color="#909090">output <em>undefined</em>, <strong>stdout</strong>&nbsp;= b'', <strong>stderr</strong>&nbsp;= 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>args&nbsp;=
(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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>cmd&nbsp;=
[r'java -jar C:\dev\git\out\artifacts\git_jar4\git.... -p C:\dev\git\src\secstruc\cb513_gor1_cb513.prd ']
<br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>output&nbsp;=
b''
<br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>returncode&nbsp;=
1
<br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>stderr&nbsp;=
None
<br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>stdout&nbsp;=
b''
<br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>with_traceback&nbsp;=
&lt;built-in method with_traceback of CalledProcessError object&gt;


<!-- 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 &lt;module&gt;
    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结束

java python jar cgi
1个回答
2
投票

不应该out = subprocess.check_output(["java -jar test.jar])

out = subprocess.check_output(["java -jar test.jar"])

(缺少关闭"

如果这不是问题,我们需要查看完整的堆栈跟踪。

© www.soinside.com 2019 - 2024. All rights reserved.