[英]how to output log info in a python cgi file?
I am using cgi (python) as a proxy to solve cross-domain issue of ajax request. 我使用cgi(python)作为代理来解决ajax请求的跨域问题。 i want to log some key info of each request, such as url, and save these info to a file on server. 我想记录每个请求的一些关键信息,例如url,并将这些信息保存到服务器上的文件中。 what should I do? 我该怎么办? I tried python logging module or File I/O module, neither seems work in this environment. 我尝试了python日志记录模块或File I / O模块,但在此环境中似乎都无法正常工作。
#!C:/Python25/python.exe -u
import urllib2
import cgi
import sys, os
f = open("./proxy.txt","a")
method = os.environ["REQUEST_METHOD"]
f.write(method + "\n")
if method == "POST":
qs = os.environ["QUERY_STRING"]
d = cgi.parse_qs(qs)
if d.has_key("url"):
url = d["url"][0]
else:
fs = cgi.FieldStorage()
url = fs.getvalue('url')
try:
y = urllib2.urlopen(url)
f.write(url + "\n")
f.close()
except Exception, E:
print "Status: 500 Unexpected Error"
print "Content-Type: text/plain"
print
print "Some unexpected error occurred. Error text was:", E
the proxy.txt file is still blank after request from client-end... 客户端请求后,proxy.txt文件仍为空白...
It's probably a combination of using a relative path and file permissions... 这可能是使用相对路径和文件权限的组合...
Try changing this line: 尝试更改此行:
f = open("./proxy.txt","a")
to an absolute path, eg 到绝对路径,例如
f = open("C:\Users\foo\Desktop\proxy.txt","a")
Make sure that the effective user running the cgi script has permission to write to the absolute path you choose... 确保运行cgi脚本的有效用户有权写入您选择的绝对路径...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.