我正在尝试在我的cherrypy Web界面上提供一个.csv文件,但出现内容长度错误:请求的资源返回的字节数比声明的Content-Length多。
我是cherrypy和Web界面开发的新手,所以我不确定是什么问题,如果我的csv为空,则将下载文件,但是如果其中包含任何信息,则会出现错误。下面是我的代码。基本上我正在查询数据库,并想将查询结果以csv格式发送给用户
类根(对象):
def index(self, *args, **kwargs):
return "index.html"
index.exposed = True
@cherrypy.expose
def query(self, subject, grade,number,semester,year):
db = MySQLdb.connect("localhost", "root", "xxx", "xxx")
cursor = db.cursor()
query="""SELECT GRADE.STUDENTID,GRADE.COURSECRN,GRADE.FINALGRADE,SEMESTER.SEMESTER,SEMESTER.YEAR FROM GRADE JOIN SEMESTER ON GRADE.COURSECRN=SEMESTER.CRN JOIN COURSE ON COURSE.CRN =SEMESTER.CRN WHERE (COURSE.SUBJECT='%s' AND COURSE.NUMBER='%s' AND SEMESTER.SEMESTER='%s' AND SEMESTER.YEAR='%s') AND (GRADE.FINALGRADE='%s') """%(subject,number,semester,year,grade)
print('Enter query 1')
values=(subject,number,semester,year,grade)
print(values)
# grade = "\'"+ grade + "\'"
print(subject,number,semester,year,grade)
cursor.execute(query)
#cursor.execute("select * from GRADE where FINALGRADE=%s;")
row = cursor.fetchone()
res = []
#while row is not None:
c=csv.writer(open("output.csv","w"))
for row in cursor:
res.append(row)
row = cursor.fetchone()
c.writerow(row)
json.dumps(row,indent=4)
print(res)
return cherrypy.lib.static.serve_file('/home/amichale/interface/output.csv','application/x-download','attachment')