1

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

3
  • 3
    What are the exact errors you are getting? Commented Oct 14, 2011 at 0:25
  • i tried File I/O lib and logging module, didn't get any error, but neither could make log file get any content... Commented Oct 14, 2011 at 5:24
  • Then please show example code using the logging module (for example), to allow effective diagnosis of the problem. Commented Oct 14, 2011 at 16:27

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your nice reply. in terms of permission, since this is a browser request, how could the app determine whether this browser request has the permission or not?
The app must be running as one effective user or another. That user needs to be able to write to the output file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.