[Tutor] RE: path in BaseHttpServer.py module

Steve Holden sholden at holdenweb.com
Mon Sep 29 07:43:35 EDT 2003


> -----Original Message-----
> From: Kirk Bailey [mailto:idiot1 at netzero.net]
> Sent: Sunday, September 28, 2003 12:21 PM
> To: Steve Holden; Tutor
> Subject: path in BaseHttpServer.py module
>
>
> Apparently BaseHTTPServer.py module defines self.path as path
> from the command
> line. relevant exerpt:
>          requestline = self.raw_requestline
>          if requestline[-2:] == '\r\n':
>              requestline = requestline[:-2]
>          elif requestline[-1:] == '\n':
>              requestline = requestline[:-1]
>          self.requestline = requestline
>          words = requestline.split()
>          if len(words) == 3:
>              [command, path, version] = words
> (line 224-232)
>   self.command, self.path, self.request_version = command,
> path, version
> (line 245)
>
Danger, danger, Will Robinson! In this statement the server is analysing
the request from the client. It should have decided its working
directory long before this, so you should *not* need to make any changes
to the *HTTPServer modules - just set your current directory as I
suggested in the previous mail (which I omitted to copy to the tutor
list, sorry, maybe you can pass on the advice when you've got it worked
out).

> OKFINE. But I want the server script to referr to a directory
> UNDER that point
> on the tree, so webpages (in fact all of the web accessable
> tree) is rooted in a
> directory UNDER that directory. This means modifying the path
> declaration in the
> module- which means editing the module, OR declaring it in
> the top level script.

Nope. The "path" you are looking at is the path *relative to the web
root* of the URI that the client is requesting - the server works this
out each time it makes a request.

> Now this gets back to how python works, espically how it
> handles building up
> it's dictionary when it imports functions in modules. If I
> redefine a function
> defined in an imported module, will this break other functions?
>
If those other functions rely on the one you changed working as it
originally did, yes. You should generally resist the temptation to poke
about randomly without a clear understanding of what's supposed to be
happening, although that *is* one technique you can use to understand
where particular things happen.

> Again, the existing script is:
> #!C:\Python22\pythonw.exe
> # COPYRIGHT 2003 Steve Holden, free for personal use.
> # Please visit http://www.holdenweb.com/ to learn more!
> #
> import CGIHTTPServer, BaseHTTPServer
> httpd=BaseHTTPServer.HTTPServer(('',8080),
> CGIHTTPServer.CGIHTTPRequestHandler)
> httpd.serve_forever()
> #
>
> Any discussion?
>
Well, only to reiterate that this script currently serves the directory
that it's run from, plus any subdirectories. So clearly we could modify
it by inserting code at the beginning to change the current directory -
that's what os.chdir() is for.

As I believe I suggested in my previous mail, one possible change would
be to look at sys.argv - the list of command-line arguments - and if
there's a second element following the program name you could use
os.chdir to change to that directory.

Since I don't want to spoon-feed you I've left the details for you to
work out, but you know you can get back to me if you can't get it
working.

regards
--
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
Interview with GvR August 14, 2003       http://www.onlamp.com/python/

>





More information about the Tutor mailing list