如何在python cgi文件中输出日志信息?
我使用cgi(python)作为代理来解决ajax请求的跨域问题。我想记录每个请求的一些关键信息,例如网址,并将这些信息保存到服务器上的文件中。我应该怎么办?我尝试了 python 日志记录模块或文件 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
客户端请求后 proxy.txt 文件仍然为空...
I am using cgi (python) as a proxy to solve cross-domain issue of ajax request. i want to log some key info of each request, such as url, and save these info to a file on server. what should I do? I tried python logging module or File I/O module, neither seems work in this environment.
#!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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它可能是使用相对路径和文件权限的组合...
尝试更改此行:
到绝对路径,例如
确保运行cgi脚本的有效用户有权写入您选择的绝对路径...
It's probably a combination of using a relative path and file permissions...
Try changing this line:
to an absolute path, e.g.
Make sure that the effective user running the cgi script has permission to write to the absolute path you choose...