python CGI struggle...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JustWant2Ask
    New Member
    • Aug 2008
    • 5

    python CGI struggle...

    Hi All,

    I am using Python and CGI to create a web server that displays charts from some data provided. Below is the code from two files. The first piece of code 'tries' to define a simple web-page, that is intended to contain an image that should be created by a script in the second file (call it a library file that knows how to plot different types of charts).

    The problem is on how to call the script located in the library file, passing also the necessary data to create the chart. I know that line 23 of the first piece of code is not correct. But I do not know how to write it either. Could anyone give me a hint?

    Dealing with python, I would assume there is a simpler way to do this, any ideas on how to do it or where to look for it will be welcome.

    Thanks!

    index.py
    Code:
    #!c:/Python24/python.exe -u
    
    import sys, os, cgi, urllib
    import cgitb; cgitb.enable()
    
    ## Import my libraries
    # Add path to search for libraries
    sys.path.append("./myLibs")
    import LibFile
    
    query = cgi.FieldStorage()
    
    TheData={'x1': range(10),'y1': range(10),'l1':'First Legend','t':'Some title','h':'Some X label','v':'Some Y label'}
    
    print "Content-type: text/html\n"
    print """
    <html>
    <head>
    	<meta http-equiv="content-type" content="text/html;charset=utf-8">
    	<title>Data analysis</title>
    </head>
    <body>
    <p><IMG SRC="myLibs/LibFile.py/myPlot(%(TheData)s)"></p>
    </body>
    </html> """ %{"TheData":urllib.quote(str(TheData))}
    LibFile.py
    Code:
    def myPlot1(myData):
        
           [...]
    ##   Create plot using matplotlib...
           [...]
    
        print "Content-Type: image/png\n"    
        pylab.savefig(sys.stdout)
    
    def myPlot2(myData):
        
           [...]
    ##   Create plot using matplotlib...
           [...]
    
        print "Content-Type: image/png\n"    
        pylab.savefig(sys.stdout)
  • Formula
    New Member
    • Aug 2008
    • 11

    #2
    why don't you use Get method

    Comment

    • JustWant2Ask
      New Member
      • Aug 2008
      • 5

      #3
      Thanks for your reply. However, I am new to cgi and web programming, and I do not understand what you mean by "using GET"... any chances for a more elaborated answer?

      Thanks!

      Comment

      • JustWant2Ask
        New Member
        • Aug 2008
        • 5

        #4
        Hi again,

        I searched a bit and found out a couple of things about GET and POST.

        Using GET will not be an option as the data to be plotted will contain potentially too many points to pass them as part of the url, and it would not be nice to see all that in the address bar either.

        So, to use post I thought I had found the way of doing it with the following lines

        Code:
        [as before]
        
        TheData={'a':'myPlot1','x1': range(10),'y1': range(10),'l1':'First Legend','t':'Some title','h':'Some X label','v':'Some Y label'}
        
        [as before]
        
        <p><IMG SRC="myLibs/LibFile.py/%(TheData)s"></p>
        
        [as before]
        But still is not correct. I checked the error log of the apache server, and found the following that I did not expect:

        Code:
        [...]GET /myLibs/LibFile.py%20[...]
        So, my two questions are:

        1 - How do I define to use POST method?
        2- is the %20 between the script name (LibFile.py) and the data (argument) correct? or should it read a space ' ' instead?

        Thanks!

        Comment

        • Formula
          New Member
          • Aug 2008
          • 11

          #5
          I think this the easier way.
          Code:
          import urllib
          
          TheData={'a':'myPlot1','x1': range(10),'y1': range(10),'l1':'First Legend','t':'Some title','h':'Some X label','v':'Some Y label'}
          
          params = urllib.urlencode(TheData)
          
          f = urllib.urlopen("http://Domain.com/myLibs/LibFile.py", params)
          
          print "Content-type: image/png\n"
          
          print f.read()
          Hope it help.

          Comment

          • JustWant2Ask
            New Member
            • Aug 2008
            • 5

            #6
            Thanks! that puts me in a complete new path...

            Where do the params go to in this case? Does it go to sys.argv[1]? how do I read them in the script that generates the chart?

            thanks!


            Originally posted by Formula
            I think this the easier way.
            Code:
            import urllib
            
            TheData={'a':'myPlot1','x1': range(10),'y1': range(10),'l1':'First Legend','t':'Some title','h':'Some X label','v':'Some Y label'}
            
            params = urllib.urlencode(TheData)
            
            f = urllib.urlopen("http://Domain.com/myLibs/LibFile.py", params)
            
            print "Content-type: image/png\n"
            
            print f.read()
            Hope it help.

            Comment

            • JustWant2Ask
              New Member
              • Aug 2008
              • 5

              #7
              I got it working!

              A quick explanation for people looking to solve the same problem. The code below in the plotting script reads the data passed on to the FieldStorage and converts it to a normal python dictionary.

              Code:
              form = cgi.FieldStorage()
              d={}
              for key in form:
                  d[key]=form[key].value
              thanks for everyones help!


              Originally posted by JustWant2Ask
              Thanks! that puts me in a complete new path...

              Where do the params go to in this case? Does it go to sys.argv[1]? how do I read them in the script that generates the chart?

              thanks!

              Comment

              Working...