1

When I run this os.walk code from my PyScripter IDE, it works just fine. The os.walk was able to traverse the remote unix path and print the directory and file names.

However, when I run this same script from the cgi-bin of my Apache server, I get no os.walk (path, dirs, files) output. I also don't get any error messages recorded in the Apache error.log file.

Note: Python and my cgi-bin are on the same Windows machine, and the remote_path is on Unix.

Why does this same code work from the console but not from the cgi-bin and what can I do to resolve this?

#!C:\Python27\python.exe -u

import os

print "Content-type: text/html\n\n";
print "<html><head>"
print "<font size=+2><B>os.walk test</B></font><br><br>";

# Remote path
remote_path = r'\\unix_server\path\2015\q1\files\na\canada'

i = 0
for (path, dirs, files) in os.walk(remote_path):
    print "Path", path
    print "<BR><BR>Dirs", dirs
    print "<BR><BR>Files", files
    i += 1
    if i >= 1:
        break
Chris Nielsen
  • 839
  • 1
  • 16
  • 31
  • 2
    Does whatever user the Apache server is running as have the appropriate permissions to access the files on the remote server? – kindall Feb 24 '15 at 23:42
  • Also: https://docs.python.org/2/library/cgitb.html is handy for debugging these kinds of things. :-) – kindall Feb 24 '15 at 23:43
  • How do I find out what user the Apache server is running as? – Chris Nielsen Feb 24 '15 at 23:45
  • http://stackoverflow.com/questions/10289133/finding-out-what-user-apache-is-running-as-in-windows and http://stackoverflow.com/questions/842059/is-there-a-portable-way-to-get-the-current-username-in-python – Paul Rooney Feb 24 '15 at 23:47
  • 1
    On Windows, open `services.msc`, find the Apache service, right-click, choose Properties, look on the Log On tab. It may be running as the local system account (that's how IIS runs), in which case it has no access to network resources. See http://httpd.apache.org/docs/2.0/platform/windows.html and search for LocalSystem for further details. – kindall Feb 24 '15 at 23:49
  • I find the python.org site unhelpful because I have never understood what is meant by the nomenclature it uses for function arguments. For example: cgitb.enable([display[, logdir[, context[, format]]]]) and cgitb.handler([info]) Why all the square brackets in the parenthesis? :( – Chris Nielsen Feb 24 '15 at 23:49
  • The square brackets indicate optional arguments (this is a very common usage). – kindall Feb 24 '15 at 23:51
  • Thanks kindall, can you tell me why it is listed as: cgitb.enable([display[, logdir[, context[, format]]]]) and not cgitb.enable([display], [logdir], [context], [format])? Still seems confusing.. – Chris Nielsen Feb 25 '15 at 00:36
  • Because your example implies that you can leave any of them out. Which isn't true. How would Python know you'd left out `display` and that your first argument was in fact `logdir` and how would it distinguish that from the situation in which you left out the first *two* arguments and the first argument was really `context`? – kindall Feb 25 '15 at 01:13
  • Thanks kindall, your questions helped me to understand this. Also the answer to the original question is indeed that Apache was not running as a user that has permissions to access the remote location. I am now looking into solving the problem of getting permissions for Apache. Thanks! – Chris Nielsen Feb 25 '15 at 15:09

1 Answers1

0

Thanks to kindall, I found out the problem was indeed that Apache didn't have the appropriate permissions.

To solve the issue, I did the following (Windows 7 OS):

  1. Opened services.msc. Start -> Run -> "services.msc"
  2. Located my Apache service in the list "Apache2". Double click it.
  3. In the Properties dialog for the Apache2 service, switch to the "Log On" tab. The radio button for "Local System account" was checked. I switched it to "This account" and clicked Browse.
  4. In the "Enter object name to select" text box, I entered the name of my network + backslash + my user name. For example NETWORK\username. Clicked OK.
  5. Then on the Log On tab, I saw that my full network email address was populated. I entered my password and password confirmation and clicked Apply.
  6. The final steps were to stop the Apache2 service and then restart the service. Once that was done, the problem was solved and my script started working from the cgi-bin. :)
Chris Nielsen
  • 839
  • 1
  • 16
  • 31