HTTP server

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • placid

    #1

    HTTP server

    Hi all,

    Ive been reading about creating a HTTP server like the one pydoc
    creates (and studying pydoc source code). What i want to know, is it
    possible to create server that creates a webpage with hyperlinks that
    communicate back to the HTTP server, where each link accessed tells the
    server to execute some arbitrary command on local machine its running
    on?

    Cheers

  • Simon Forman

    #2
    Re: HTTP server

    placid wrote:[color=blue]
    > Hi all,
    >
    > Ive been reading about creating a HTTP server like the one pydoc
    > creates (and studying pydoc source code). What i want to know, is it
    > possible to create server that creates a webpage with hyperlinks that
    > communicate back to the HTTP server, where each link accessed tells the
    > server to execute some arbitrary command on local machine its running
    > on?
    >
    > Cheers[/color]

    Yes. It is possible.


    Ok, seriously, I don't know how pydoc does it, but when I need a
    quick-and-dirty http server [written in python] I use something like
    this:

    from BaseHTTPServer import HTTPServer
    from SimpleHTTPServe r import SimpleHTTPReque stHandler

    HTTPServer(('', 8000), SimpleHTTPReque stHandler).serv e_forever()

    For what you're asking about you'd probably want to use the
    CGIHTTPRequestH andler from the CGIHTTPServer module instead. Check out


    Then you'd just write one or more cgi scripts (they can be in python
    IIRC) to run the commands you want.

    HTH,
    ~Simon

    Comment

    • placid

      #3
      Re: HTTP server


      Simon Forman wrote:[color=blue]
      >
      >
      > Ok, seriously, I don't know how pydoc does it, but when I need a
      > quick-and-dirty http server [written in python] I use something like
      > this:
      >
      > from BaseHTTPServer import HTTPServer
      > from SimpleHTTPServe r import SimpleHTTPReque stHandler
      >
      > HTTPServer(('', 8000), SimpleHTTPReque stHandler).serv e_forever()
      >
      > For what you're asking about you'd probably want to use the
      > CGIHTTPRequestH andler from the CGIHTTPServer module instead. Check out
      > http://docs.python.org/lib/module-CGIHTTPServer.html[/color]

      This is what i was after, thanks for the tip.

      [color=blue]
      > Then you'd just write one or more cgi scripts (they can be in python
      > IIRC) to run the commands you want.[/color]

      Im having trouble running the following cgi script on windows

      <code>

      #!c:/Python/python.exe -u

      text = """Content-type: text/html

      <TITLE> CGI 101 </TITLE>
      <H1>A Second CGI script</H1>
      <HR>
      <P>Hello, CGI World!</P>
      """
      print text

      </code>


      using this http server from


      <code>

      import CGIHTTPServer
      import BaseHTTPServer

      class Handler(CGIHTTP Server.CGIHTTPR equestHandler):
      cgi_directories = ["/cgi"]

      PORT = 8000

      httpd = BaseHTTPServer. HTTPServer(("", PORT), Handler)
      print "serving at port", PORT
      httpd.serve_for ever()

      </code>

      i get the error number 403, when i try to access the cgi script which
      is located in a subdirectory called cgi where this file is (http
      server). I have a feeling that i need to change the Handler class or
      something, implement , but i couldnt find any examples other then this
      from eff-bot.

      It could also be this line of code, that a google search turned up, its
      the way of running cgi scripts on windows

      #!c:/Python/python.exe -u


      Cheers

      Comment

      • Simon Forman

        #4
        Re: HTTP server

        placid wrote:[color=blue]
        > Simon Forman wrote:[color=green]
        > >[/color][/color]
        ....[color=blue][color=green]
        > > For what you're asking about you'd probably want to use the
        > > CGIHTTPRequestH andler from the CGIHTTPServer module instead. Check out
        > > http://docs.python.org/lib/module-CGIHTTPServer.html[/color]
        >
        > This is what i was after, thanks for the tip.
        >[/color]

        You're welcome, my pleasure. : )

        ....[color=blue]
        >
        > Im having trouble running the following cgi script on windows
        >
        > <code>
        >
        > #!c:/Python/python.exe -u
        >
        > text = """Content-type: text/html
        >
        > <TITLE> CGI 101 </TITLE>
        > <H1>A Second CGI script</H1>
        > <HR>
        > <P>Hello, CGI World!</P>
        > """
        > print text
        >
        > </code>
        >
        >
        > using this http server from
        > http://effbot.org/librarybook/cgihttpserver.htm
        >
        > <code>
        >
        > import CGIHTTPServer
        > import BaseHTTPServer
        >
        > class Handler(CGIHTTP Server.CGIHTTPR equestHandler):
        > cgi_directories = ["/cgi"]
        >
        > PORT = 8000
        >
        > httpd = BaseHTTPServer. HTTPServer(("", PORT), Handler)
        > print "serving at port", PORT
        > httpd.serve_for ever()
        >
        > </code>
        >
        > i get the error number 403, when i try to access the cgi script which
        > is located in a subdirectory called cgi where this file is (http
        > server). I have a feeling that i need to change the Handler class or
        > something, implement , but i couldnt find any examples other then this
        > from eff-bot.
        >
        > It could also be this line of code, that a google search turned up, its
        > the way of running cgi scripts on windows
        >
        > #!c:/Python/python.exe -u
        >
        >
        > Cheers[/color]


        Your cgi and server scripts look fine to me.

        Some diagnostic questions:

        What is the filename of your cgi script?
        (It must end in ".py" or ".pyw" for CGIHTTPRequestH andler to recognize
        it as a python script. See the is_python() method in the handler
        class. Also, in this case the "#!c:/Python/python.exe -u" line isn't
        needed, but it doesn't hurt. CGIHTTPRequestH andler should find the
        python interpreter for you without it.)

        What was the exact URL that you're using to try to reach your script?
        (It should look something like:
        "http://localhost:8000/cgi/myscript.py")

        What was the error message that accompanied the 403 error?

        Did you start the server script from the directory it's in?

        What was the log output from your server script when you tried to
        access the cgi script?

        Peace,
        ~Simon

        Comment

        • placid

          #5
          Re: HTTP server


          Simon Forman wrote:[color=blue]
          > placid wrote:[color=green]
          > > Simon Forman wrote:[color=darkred]
          > > >[/color][/color]
          > ...[color=green][color=darkred]
          > > > For what you're asking about you'd probably want to use the
          > > > CGIHTTPRequestH andler from the CGIHTTPServer module instead. Check out
          > > > http://docs.python.org/lib/module-CGIHTTPServer.html[/color]
          > >
          > > This is what i was after, thanks for the tip.
          > >[/color]
          >
          > You're welcome, my pleasure. : )
          >
          > ...[color=green]
          > >
          > > Im having trouble running the following cgi script on windows
          > >
          > > <code>
          > >
          > > #!c:/Python/python.exe -u
          > >
          > > text = """Content-type: text/html
          > >
          > > <TITLE> CGI 101 </TITLE>
          > > <H1>A Second CGI script</H1>
          > > <HR>
          > > <P>Hello, CGI World!</P>
          > > """
          > > print text
          > >
          > > </code>
          > >
          > >
          > > using this http server from
          > > http://effbot.org/librarybook/cgihttpserver.htm
          > >
          > > <code>
          > >
          > > import CGIHTTPServer
          > > import BaseHTTPServer
          > >
          > > class Handler(CGIHTTP Server.CGIHTTPR equestHandler):
          > > cgi_directories = ["/cgi"]
          > >
          > > PORT = 8000
          > >
          > > httpd = BaseHTTPServer. HTTPServer(("", PORT), Handler)
          > > print "serving at port", PORT
          > > httpd.serve_for ever()
          > >
          > > </code>
          > >
          > > i get the error number 403, when i try to access the cgi script which
          > > is located in a subdirectory called cgi where this file is (http
          > > server). I have a feeling that i need to change the Handler class or
          > > something, implement , but i couldnt find any examples other then this
          > > from eff-bot.
          > >
          > > It could also be this line of code, that a google search turned up, its
          > > the way of running cgi scripts on windows
          > >
          > > #!c:/Python/python.exe -u
          > >
          > >
          > > Cheers[/color]
          >
          >
          > Your cgi and server scripts look fine to me.
          >
          > Some diagnostic questions:
          >
          > What is the filename of your cgi script?
          > (It must end in ".py" or ".pyw" for CGIHTTPRequestH andler to recognize
          > it as a python script. See the is_python() method in the handler
          > class. Also, in this case the "#!c:/Python/python.exe -u" line isn't
          > needed, but it doesn't hurt. CGIHTTPRequestH andler should find the
          > python interpreter for you without it.)[/color]

          The file was named test.cgi. I changed it too test.py and it worked
          [color=blue]
          >
          > What was the exact URL that you're using to try to reach your script?
          > (It should look something like:
          > "http://localhost:8000/cgi/myscript.py")[/color]

          i was trying to access it at http://localhost:8000/test.cgi
          which i now know is wrong.
          [color=blue]
          >
          > What was the error message that accompanied the 403 error?[/color]

          [color=blue]
          >
          > Did you start the server script from the directory it's in?[/color]

          i started the server one directory above the cgi directory
          [color=blue]
          > What was the log output from your server script when you tried to
          > access the cgi script?[/color]

          The error message outputted by the server was
          localhost - - [26/Jun/2006 09:56:53] code 403, message CGI script is
          not a plain file ('/cgi/')


          Thanks for the help. I got it to work now.

          Cheers

          Comment

          • Simon Forman

            #6
            Re: HTTP server


            placid wrote:[color=blue]
            > Simon Forman wrote:[/color]
            ....[color=blue]
            >
            > The file was named test.cgi. I changed it too test.py and it worked
            >[/color]

            Awesome! Glad to hear it.

            ....[color=blue]
            >
            > Thanks for the help. I got it to work now.
            >[/color]

            You're welcome. I'm glad I could help you. :-D


            Peace,
            ~Simon

            Comment

            • placid

              #7
              Re: HTTP server


              Simon Forman wrote:[color=blue]
              > ...[color=green]
              > >[/color]
              >
              > Awesome! Glad to hear it.
              >
              > ...[color=green]
              > >
              > > Thanks for the help. I got it to work now.
              > >[/color]
              >
              > You're welcome. I'm glad I could help you. :-D
              >[/color]

              Im having trouble with the following code for handling GET requests
              from a client to my HTTP server. What i want to do is restrict access
              only to a folder and contents within this folder. But when trying to
              open files (text files) i get file not found error from send_head()
              method of SimpleHTTPServe r. The reason behind this is when opening the
              file the path to the file is only C:\file.txt when it should be
              C:\folder\file. txt. And when i remove the code that checks if path
              contains "txt" it works (i can access files without errors).

              Any help will be greatly appreciated!

              <code>
              def list_directory( self, path):
              """Helper to produce a directory listing (absent index.html).

              Return value is either a file object, or None (indicating an
              error). In either case, the headers are sent, making the
              interface the same as for send_head().

              """
              f = StringIO()

              p = self.translate_ path(self.path)
              if p.find("txt") == -1:
              f.write("<title >httpserver.p y: Access Denied</title>" )
              f.write("<h2>ht tpserver.py: Access Denied</h2>" )
              else:
              try:
              list = os.listdir(path )
              except os.error:
              self.send_error (404, "No permission to list directory")
              return None

              list.sort(key=l ambda a: a.lower())

              displaypath = cgi.escape(urll ib.unquote(self .path))
              f.write("<title >Directory listing for %s</title>\n" %
              displaypath)
              f.write("<h2>Di rectory listing for %s</h2>\n" %
              displaypath)
              f.write("<hr>\n <ul>\n")
              for name in list:
              fullname = os.path.join(pa th, name)
              displayname = linkname = name
              # Append / for directories or @ for symbolic links
              if os.path.isdir(f ullname):
              displayname = name + "/"
              linkname = name + "/"
              if os.path.islink( fullname):
              displayname = name + "@"
              # Note: a link to a directory displays with @ and
              links with /
              f.write('<li><a href="%s">%s</a>\n'
              % (urllib.quote(l inkname),
              cgi.escape(disp layname)))
              f.write("</ul>\n<hr>\n")

              length = f.tell()
              f.seek(0)
              self.send_respo nse(200)
              self.send_heade r("Content-type", "text/html")
              self.send_heade r("Content-Length", str(length))
              self.end_header s()
              return f
              </code>

              Comment

              Working...