如何在python cgi文件中输出日志信息?

发布于 2024-12-09 21:34:21 字数 787 浏览 0 评论 0原文

我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2024-12-16 21:34:21

它可能是使用相对路径和文件权限的组合...

尝试更改此行:

f = open("./proxy.txt","a")

到绝对路径,例如

f = open("C:\Users\foo\Desktop\proxy.txt","a")

确保运行cgi脚本的有效用户有权写入您选择的绝对路径...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文