create a web service for python application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • susanne
    New Member
    • Jul 2009
    • 5

    create a web service for python application

    Hi, I am very new to the python web applications. can some one help.
    I have a python application with some functions in it.

    1) The application reads an input file ( say, IN.txt).
    the IN.txt file contains names of three other files( x.dat, y.txt, z.dat) that are
    in the same directory.

    2) then the application reads the data in the files x.dat, y.txt, z.dat and performs some calculations.

    3) outputs the results in a file called out.txt

    The application is running perfectly from command mode. I need to make it a web application so that it can be run in a browser from anywhere.
    I tried with cherrypy and django tutorials, but could not succeed, as i am completely new to this field. can some one help with steps to proceed.

    Your help is highly appreciated.
    Thanks in advance!
    Susanne
  • kudos
    Recognized Expert New Member
    • Jul 2006
    • 127

    #2
    It sound like what you need is a cgi-script. This is luckily quite simple.

    here is a template:

    Code:
    #!/usr/bin/python
    
    import os
    import cgi
    
    print "Content-Type: text/plain\n\n"
    
    # your script here...
    print "hi how are you!"
    Ofcourse there are some details:

    1. the file should not have an .py extension, but rather a .cgi extension
    2. the file should have the correct properties (i.e. chmod a+x <filename.cgi >)
    3. it should be placed in a "cgi-bin" directory (under your www folder)
    4. then you would access it like http://www.yourdomain.com/cgi-bin/yourpythonfile.cgi

    (It sound like you were planning to do this in a unix enviroment)

    There are multiple other options, but this one is quite simple.

    -kudos

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I'm just going to add a bit to Kudos's answer.

      The output that your python script should produce should be HTML since it's going to be displayed in a web browser.

      I would recommend keeping a clear separation between your current logic and the HTML or else it can get very messy to read.

      Make a function that builds the HTML so that it's easier to understand what's going on.

      For example:
      Code:
      #!/usr/bin/python
      
      import os
      import cgi
      
      def getfileoutput():
        # This is where you grab the data from the files
      
      def output(fileoutput = ""):
         #This produces the HTML that is to be sent to the browser
         htmlOutput = '<html>'
         htmlOutput  += '   <head>'
         htmlOutput  += '      <title></title>'
         htmlOutput  += '   </head>'
         htmlOutput  += '   <body>'
         htmlOutput  += '      <div>'
         htmlOutput  +=  fileoutput
         htmlOutput  += '      </div>'
         htmlOutput  += '   </body>'
         htmlOutput += '</html>'
         return htmlOutput
      
      print output(getfileoutput())

      Comment

      • kudos
        Recognized Expert New Member
        • Jul 2006
        • 127

        #4
        That is very true! however do not forget this part:

        Code:
        print "Content-Type: text/plain\n\n"
        -kudos

        Originally posted by Frinavale
        I'm just going to add a bit to Kudos's answer.

        The output that your python script should produce should be HTML since it's going to be displayed in a web browser.

        I would recommend keeping a clear separation between your current logic and the HTML or else it can get very messy to read.

        Make a function that builds the HTML so that it's easier to understand what's going on.

        For example:
        Code:
        #!/usr/bin/python
        
        import os
        import cgi
        
        def getfileoutput():
          # This is where you grab the data from the files
        
        def output(fileoutput = ""):
           #This produces the HTML that is to be sent to the browser
           htmlOutput = '<html>'
           htmlOutput  += '   <head>'
           htmlOutput  += '      <title></title>'
           htmlOutput  += '   </head>'
           htmlOutput  += '   <body>'
           htmlOutput  += '      <div>'
           htmlOutput  +=  fileoutput
           htmlOutput  += '      </div>'
           htmlOutput  += '   </body>'
           htmlOutput += '</html>'
           return htmlOutput
        
        print output(getfileoutput())

        Comment

        • susanne
          New Member
          • Jul 2009
          • 5

          #5
          create a web service for python application

          Hi kudos and Frinavale,
          Thanks a lot for your quick responses!
          I did followed the steps as you suggested.

          1) I inserted my complete python script in between line numbers 21 and 23 of Frinavale's code in the reply. after that i also inserted the "Content-Type" as Kudos suggested.

          2) I renamed the file with .cgi extenstion.

          3) I kept the .cgi file under the cgi-bin directory.
          ( Windows-XP/program files/apache 2.2/cgi-bin)

          4) I copied the other files also to the same directory( In.txt, x.dat, y.txt, z.dat)
          as the python script needed them.

          when i executed the code in internet explorer or mozilla firefox




          either I am getting
          file not found error
          OR
          Internal Server Error: The server encountered an internal error or misconfiguratio n and was unable to complete your request.

          When I check http://localhost:80 Then the server shows "It Is Working"

          I am confused. Did I do some thing wrong with hte code. Or do I need to add anything else.

          Please suggest me
          Thanks
          Susanne

          Comment

          • kudos
            Recognized Expert New Member
            • Jul 2006
            • 127

            #6
            Ok, I see there is an apache server on a windows platform. Then you would need to find out how (and if) it is configured for python and cgi. You would also need be sure that the various files (.cgi) and (cgi-bin) directory is have the correct access rights.

            However, I have an alternative solution for you, python has a built in webserver!

            Code:
            from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
            
            class myserver(BaseHTTPRequestHandler):
             def do_GET(self):
              self.send_response(200)
              self.send_header("content-type","text/html")
              self.end_headers()
              self.wfile.write("your output as a html-string here...")
              return
            try:
             s = HTTPServer(('',2606),myserver)
             s.serve_forever()
            except KeyboardInterrupt:
             s.socket_close()
            Now, type http://localhost:2606 in your browser to see the webserver in action

            -kudos


            Originally posted by susanne
            Hi kudos and Frinavale,
            Thanks a lot for your quick responses!
            I did followed the steps as you suggested.

            1) I inserted my complete python script in between line numbers 21 and 23 of Frinavale's code in the reply. after that i also inserted the "Content-Type" as Kudos suggested.

            2) I renamed the file with .cgi extenstion.

            3) I kept the .cgi file under the cgi-bin directory.
            ( Windows-XP/program files/apache 2.2/cgi-bin)

            4) I copied the other files also to the same directory( In.txt, x.dat, y.txt, z.dat)
            as the python script needed them.

            when i executed the code in internet explorer or mozilla firefox




            either I am getting
            file not found error
            OR
            Internal Server Error: The server encountered an internal error or misconfiguratio n and was unable to complete your request.

            When I check http://localhost:80 Then the server shows "It Is Working"

            I am confused. Did I do some thing wrong with hte code. Or do I need to add anything else.

            Please suggest me
            Thanks
            Susanne

            Comment

            • susanne
              New Member
              • Jul 2009
              • 5

              #7
              HI kudos, thanks a lot again for your help

              I tried configuring apache server to be able to work with cgi as well.
              And I tried with the built-in server script you have provided.

              The server is running but i can see only
              " your output as a html-string here..." in the browser.

              I tried even this simple cgi script (hi.cgi) to execute
              (http://localhost:2606/hi.cgi )
              but i see only the same message as above.

              #!/usr/bin/python
              import os
              import cgi
              print "Content-Type: text/plain\n\n"
              print "hello!"

              Comment

              • kudos
                Recognized Expert New Member
                • Jul 2006
                • 127

                #8
                Hi,
                what you should do is the following:

                place your program between :

                Code:
                self.end_headers()
                -- your program here ---
                return
                then use the following to output from your program:

                Code:
                self.wfile.write("your output as a html-string here...")
                I have created a simple example, that does something, and later puts it to the webbrowser

                Code:
                from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
                
                class myserver(BaseHTTPRequestHandler):
                 def do_GET(self):
                  self.send_response(200)
                  self.send_header("content-type","text/html")
                  self.end_headers()
                  
                  # this code do 'something'
                  
                  html=""
                  for i in range(32):
                   html+="<tr>"
                   for j in range(32):
                    html+="<td bgcolor=\""+hex(int(((j^i) / 64.0)*256))+"\"></td>&nbsp;</td>"
                  html+="</tr>" 
                  
                  # writes all to string called html
                  
                  html = "<html></body><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" height=\"100%\">" +html+"</table></body></html>" 
                  
                  # writes to the screen/webclient
                  
                  self.wfile.write(html)
                  return
                  
                try:
                 s = HTTPServer(('',2606),myserver)
                 s.serve_forever()
                except KeyboardInterrupt:
                 s.socket_close()
                Please note, this webserver doesn't have the features of a normal webserver, but is more intended to do the following:

                1. do some kind of calculations (read files, add numbers etc, etc)
                2. display it to a webbrowser (as a webserver)

                (PS. to set ut apache for cgi-script, if I remeber correctly you would need to change something called scriptalias)

                -kudos



                Originally posted by susanne
                HI kudos, thanks a lot again for your help

                I tried configuring apache server to be able to work with cgi as well.
                And I tried with the built-in server script you have provided.

                The server is running but i can see only
                " your output as a html-string here..." in the browser.

                I tried even this simple cgi script (hi.cgi) to execute
                (http://localhost:2606/hi.cgi )
                but i see only the same message as above.

                #!/usr/bin/python
                import os
                import cgi
                print "Content-Type: text/plain\n\n"
                print "hello!"

                Comment

                • martinscott
                  New Member
                  • Jul 2009
                  • 1

                  #9
                  Change bytes in a file stream

                  I am reading in bytes from a hdd image (dd). I want to look for the header of a JPEG (FFD8 4a 46 49 46) and replace all the bytes before the footer (FFD9) with 0. My questions is: How can I make a change to the original file stream?

                  Comment

                  • kudos
                    Recognized Expert New Member
                    • Jul 2006
                    • 127

                    #10
                    First, I happen to have done some jpeg work earlier, are you sure that your header is correct? (I thought it was a bit longer) anyway, here is some code to get you started:

                    Code:
                    import os
                    import stat
                    f = file('yourimage.jpg', 'rb')
                    
                    pat = [0xFF,0xD8,0xff,0xe0,0x0,0x10,0x4a,0x46,0x49,0x46] # i thought that this was the pattern...
                    pati = 0
                    
                    s = os.stat('yourimage')
                    size = s[stat.ST_SIZE]
                    for i in range(size):
                     v = f.read(1)
                     if(pati > len(pat)):
                      if(v == pat[pati]):
                       pati+=1
                      else:
                       pati=0
                     else:
                      if(i < size - 2):
                       # write the 0's here or simillar (then you would need to write 'ab' instead above
                       pass
                    f.close()
                    -kudos


                    Originally posted by martinscott
                    I am reading in bytes from a hdd image (dd). I want to look for the header of a JPEG (FFD8 4a 46 49 46) and replace all the bytes before the footer (FFD9) with 0. My questions is: How can I make a change to the original file stream?

                    Comment

                    • susanne
                      New Member
                      • Jul 2009
                      • 5

                      #11
                      create a web service for python application

                      hi Kudos,
                      I sorted out the problem with my apache server on windows. Finally the apache server is running with test cgi script. But my actual python application( now, cgi application) is still not running. Actually in the original application the input file is passed as an argument in the command line (c:/ python application.py input).
                      Is it the problem when i run cgi script in the browser without passing the argument??
                      Thank you.
                      Susanne

                      Comment

                      • kudos
                        Recognized Expert New Member
                        • Jul 2006
                        • 127

                        #12
                        Yes, you would need give the script an argument. To do this, there are multiple options, I will show the cgi-variant. This mean that you would give the input as an argument from the webbrowser

                        For instance, normally you would do something like this:
                        Code:
                        c:/ python application.py input
                        (right?)

                        what you want to do now is the following, is to type in the following into your webbrowser:

                        Code:
                        http://localhost/cgi-bin/application.cgi?myinput=input
                        To read the input to this webservice (which by the way is myinput=input) you would need to add the following:

                        Code:
                        import cgi
                        form = cgi.FieldStorage()
                        input = form["myinput"]
                        Ok?

                        -kudos





                        Originally posted by susanne
                        hi Kudos,
                        I sorted out the problem with my apache server on windows. Finally the apache server is running with test cgi script. But my actual python application( now, cgi application) is still not running. Actually in the original application the input file is passed as an argument in the command line (c:/ python application.py input).
                        Is it the problem when i run cgi script in the browser without passing the argument??
                        Thank you.
                        Susanne

                        Comment

                        • susanne
                          New Member
                          • Jul 2009
                          • 5

                          #13
                          Hi Kudos,
                          Thanks a lot for all your help so far. I think the problem was with my .cgi file so far. So, I configured the apache so that it directly excuetes my python script(.py) from the brwoser.


                          however, the input (argument) is not read by the python script when i supplied the input file as shown in URL above. Probably because it is again cgi specific.
                          I placed this code inside my main python script
                          *************** ********
                          import cgi
                          form = cgi.FieldStorag e()
                          input = form["myinput"]
                          *************** **********
                          any hint pls? thanks

                          Comment

                          • kudos
                            Recognized Expert New Member
                            • Jul 2006
                            • 127

                            #14
                            The problem is :

                            Code:
                            input = form["myinput"]
                            it should rather be:

                            Code:
                            input = form["myinput"].value
                            -kudos


                            Originally posted by susanne
                            Hi Kudos,
                            Thanks a lot for all your help so far. I think the problem was with my .cgi file so far. So, I configured the apache so that it directly excuetes my python script(.py) from the brwoser.


                            however, the input (argument) is not read by the python script when i supplied the input file as shown in URL above. Probably because it is again cgi specific.
                            I placed this code inside my main python script
                            *************** ********
                            import cgi
                            form = cgi.FieldStorag e()
                            input = form["myinput"]
                            *************** **********
                            any hint pls? thanks

                            Comment

                            Working...