这是错误:
127.0.0.1 - - [22/Jun/2015 17:15:21] "POST /cgi-bin/get_tran_data.py HTTP/1.1" 2
00 -
127.0.0.1 - - [22/Jun/2015 17:15:21] command: C:\Python34\python.exe -u C:\Pytho
n34\ProjectShivam\webapp\cgi-bin\get_tran_data.py ""
127.0.0.1 - - [22/Jun/2015 17:15:22] b'Traceback (most recent call last):\r\n F
ile "C:\\Python34\\ProjectShivam\\webapp\\cgi-bin\\get_tran_data.py", line 20, i
n <module>\r\n with open("C:\\Python34\\ProjectShivam\\webapp\\cgi-bin\\tran_
add_success.py") as g:\r\nOSError: [Errno 22] Invalid argument: \'C:\\\\Python34
\\\\ProjectShivam\\\\webapp\\\\cgi-bin\\tran_add_success.py\'\r\n'
127.0.0.1 - - [22/Jun/2015 17:15:22] CGI script exit status 0x1
get_tran_data.py:
import cgi
import yate
import sqlite3
import sys
connection = sqlite3.connect('users.sqlite')
cursor = connection.cursor()
print('Content-type:text/html')
form=cgi.FieldStorage()
doctype=form['doctype'].value
docno=form['docno'].value
docdate=form['docdate'].value
bincard=form['bincard'].value
rate=form['rate'].value
quantity=form['qty'].value
cursor.execute("INSERT INTO TRAN(DOCTYPE,DOCNO,DOCDATE,BINCARD,QTY,RATE) VALUES (?,?,?,?,?,?)",(doctype,docno,docdate,bincard,rate,quantity))
connection.commit()
with open("C:\Python34\ProjectShivam\webapp\cgi-bin\tran_add_success.py") as g:
code = compile(g.read(),"tran_add_success.py", 'exec')
exec(code)
当我从get_tran_data.py中删除open语句时,脚本成功运行。我在另一个脚本中使用了类似的open语句并且运行成功,但在这里我不知道是什么问题?
编辑:
这是get_opb_data.py,代码类似但运行成功的脚本:
import cgi
import yate
import sqlite3
import sys
connection = sqlite3.connect('users.sqlite')
cursor = connection.cursor()
print('Content-type:text/html')
form=cgi.FieldStorage()
bincard=form['bincard'].value
desc=form['desc'].value
loc=form['loc'].value
qty=form['qty'].value
rate=form['rate'].value
Value=form['value'].value
currate=form['currate'].value
curqty=form['curqty'].value
cursor.execute("INSERT INTO OPB(Bincard,Description,Location,Quantity,Rate,Value,CurRate,CurQty) VALUES (?,?,?,?,?,?,?,?)",(bincard,desc,loc,qty,rate,Value,currate,curqty))
connection.commit()
with open("C:\Python34\ProjectShivam\webapp\cgi-bin\opb_add_success.py") as f:
code = compile(f.read(), "opb_add_success.py", 'exec')
exec(code)
此外,tran_add_success.py文件存在,我已经确定了。我不明白为什么会这样,一旦脚本运行得很好而另一个不是。
tran.html:
<html>
<head>
<title>Transaction</title>
<link type="text/css" rel="stylesheet" href="coach.css" />
</head>
<body>
<img src="images/logo-cel-transparent_0.png" width="74" height="64"><strong><img src="images/logo-cel-transparent_0.png" alt="Cel logo" width="74" height="64" align="right">
</strong>
<h1 align="center"><strong>Central Electronics Limited</strong></h1>
<p> </p>
<h2 align="center">Storage Management System</h2>
<p> </p>
<div align="center"><strong>Transaction!!</strong></div>
<p align="left"> </p>
<p align="center">
<form action="cgi-bin/get_tran_data.py" method="post">
<div align="center">DocType :
<input type="text" name="doctype">
DocNo :
<input type="text" name="docno">
DocDate :
<input type="text" name="docdate">
<br><br>
BinCard :
<input type="text" name="bincard">
Rate :
<input type="text" name="rate">
Quantity :
<input type="text" name="qty">
<br>
<p align="center"><input type="submit" value="Submit"></p>
</div>
</form>
</p>
</body>
</html>
当我点击tran.html上的提交按钮时,get_tran_data.py会运行。现在,它从表单中获取数据并将其保存到数据库。我在get_tran_data.py中使用open语句,以便运行另一个在屏幕上显示成功消息的脚本。