开发者

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. 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...


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, e.g.

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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜