Question -- Running Programming Python Examples

Discussion in 'Python' started by chernevik@gmail.com, Aug 4, 2007.

  1. Guest

    Here is a newbie question: how do I get the server examples in the
    Preview chapter of "Progamming Python" (Lutz) to run? The code is
    supposed to be a little webserver on which to run examples, but when I
    run it it I get "permission denied". Running it as root gets "address
    already in use".

    Here is the code (it's example 2.32); comments are from Lutz, not me:

    webdir = '.' # where your html files and cgi-bin script directory
    live

    port = 80 # default http://localhost/, else use http://localhost:xxxx/



    import os, sys

    from BaseHTTPServer import HTTPServer

    from CGIHTTPServer import CGIHTTPRequestHandler



    # hack for Windows: os.environ not propogated

    [deleted, I'm running linux]
    .. . .

    os.chdir(webdir) # run in html
    root dir

    srvraddr = ("", port) # my hostname,
    portnumber

    srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)

    srvrobj.serve_forever() # run as
    perpetual demon


    END CODE


    My other python scripts run fine.

    I'm running linux (debian). I'm not running an webserver (that I know
    of anyway).

    I've fiddled with adding the subdirectory with the code to the python
    path, but this doesn't seem to help either. I'm new to all this, but
    I've been able to make the other stuff work, and I can't even find the
    beginning of what I'm supposed to research to fix this.

    Any help would be greatly appreciated, and thanks for your time and
    patience.
     
    , Aug 4, 2007
    #1
    1. Advertising

  2. wrote:

    > Here is a newbie question: how do I get the server examples in the
    > Preview chapter of "Progamming Python" (Lutz) to run? The code is
    > supposed to be a little webserver on which to run examples, but when I
    > run it it I get "permission denied". Running it as root gets "address
    > already in use".


    The first error is because non-root users cannot bind to ports lower
    than 1024. The second error means just what it says: The address is
    already in use, so you can't bind to port 80. Something else is already
    bound to it; probably you have an HTTP server already running as part of
    your default software installation and don't realize it.

    Choose another port.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
    If love is the answer, could you rephrase the question?
    -- Lily Tomlin
     
    Erik Max Francis, Aug 4, 2007
    #2
    1. Advertising

  3. Gary Herron Guest

    wrote:
    > Here is a newbie question: how do I get the server examples in the
    > Preview chapter of "Progamming Python" (Lutz) to run? The code is
    > supposed to be a little webserver on which to run examples, but when I
    > run it it I get "permission denied". Running it as root gets "address
    > already in use".
    >
    > Here is the code (it's example 2.32); comments are from Lutz, not me:
    >
    > webdir = '.' # where your html files and cgi-bin script directory
    > live
    >
    > port = 80 # default http://localhost/, else use http://localhost:xxxx/
    >

    There's the trouble. You need special permission to open any of the
    ports up through 1023. As root, you have permission, but apparently
    some process already has that port opened, almost certainly a web server
    you start up at boot time, probably apache.

    So either kill off the web server that has port 80 opened, or better
    yet, just change the port to something else. A common choice is port
    8080. This does not require superuser permission, and is probably free.
    port = 8080

    If you do that, then you access the server on port 8080 with url's that
    look like this:

    http://localhost:8080/what/ever/..., or
    http://machine-name:8080/what/ever/...,

    Gary Herron
    >
    >
    > import os, sys
    >
    > from BaseHTTPServer import HTTPServer
    >
    > from CGIHTTPServer import CGIHTTPRequestHandler
    >
    >
    >
    > # hack for Windows: os.environ not propogated
    >
    > [deleted, I'm running linux]
    > . . .
    >
    > os.chdir(webdir) # run in html
    > root dir
    >
    > srvraddr = ("", port) # my hostname,
    > portnumber
    >
    > srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
    >
    > srvrobj.serve_forever() # run as
    > perpetual demon
    >
    >
    > END CODE
    >
    >
    > My other python scripts run fine.
    >
    > I'm running linux (debian). I'm not running an webserver (that I know
    > of anyway).
    >
    > I've fiddled with adding the subdirectory with the code to the python
    > path, but this doesn't seem to help either. I'm new to all this, but
    > I've been able to make the other stuff work, and I can't even find the
    > beginning of what I'm supposed to research to fix this.
    >
    > Any help would be greatly appreciated, and thanks for your time and
    > patience.
    >
    >
     
    Gary Herron, Aug 4, 2007
    #3
  4. Guest

    That fixed it, and Gary's item on pointing my browser to the proper
    port answered the next question percolating in my mind. It now runs
    as advertised.

    Thanks to you both!
     
    , Aug 4, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    51
    Views:
    1,773
    Roedy Green
    Sep 25, 2005
  2. Ram Ravi
    Replies:
    0
    Views:
    332
    Ram Ravi
    Aug 28, 2004
  3. Victor Kryukov
    Replies:
    34
    Views:
    997
    Victor Kryukov
    May 25, 2007
  4. Replies:
    3
    Views:
    313
    Simon Brunning
    Jun 6, 2009
  5. Chris Rebert
    Replies:
    1
    Views:
    387
    John Machin
    Jun 6, 2009
Loading...

Share This Page