|
From: Steve C. <sc...@ja...> - 2009-11-15 06:05:34
|
Am trying to use gnuplot as a subprocess to a CGI script written in python (similar to the perl script on p. 241 of "Gnuplot in Action"). The python script itself follows the pattern of the python script on p.230-231, that is gp = os.popen( ' /usr/bin/gnuplot', 'w') ... plotting commands gp.close() The cgi script produces a 500 error. But, if I comment out the gp.close() command, it works perfectly, and produces the graph I want. What is going on here? |
|
From: Thomas S. <t.s...@fz...> - 2009-11-15 13:01:06
|
there should be information about what caused the error in the error-log of the http-server. stevecoh1 wrote: > > Am trying to use gnuplot as a subprocess to a CGI script written in > python (similar to the perl script on p. 241 of "Gnuplot in Action"). > > The python script itself follows the pattern of the python script on > p.230-231, that is > > gp = os.popen( ' /usr/bin/gnuplot', 'w') > ... plotting commands > gp.close() > > The cgi script produces a 500 error. > > But, if I comment out the gp.close() command, it works perfectly, and > produces the graph I want. What is going on here? > > > > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and focus > on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > Gnuplot-info mailing list > Gnu...@li... > https://lists.sourceforge.net/lists/listinfo/gnuplot-info > > -- View this message in context: http://old.nabble.com/strange-behavior-on-calling-gnuplot-from-cgi-script-written-in-python-tp26356744p26358940.html Sent from the Gnuplot - User mailing list archive at Nabble.com. |
|
From: Steve C. <sc...@ja...> - 2009-11-15 16:59:21
|
Here's the error:
[error] [client ::1] malformed header from script. Bad header=\x89PNG:
usage_graph.cgi
I don't understand this. How could closing the gnuplot process create a
malformed header yet leaving it open produces usable output? Still, I
guess that's a clue. I just don't know what it means.
Here is the guts of the script, deliberately leaving out details:
#!/usr/bin/python
import cgi
import MySQLdb
import os
parms = cgi.FieldStorage()
print "Content-Type: image/png" # This is an image
print # blank line, end of headers
# open database connection
...
# create a cursor
cursor = db.cursor()
# execute SQL query
cursor.execute(query % (userid))
# get the resultset as a tuple
result = cursor.fetchall()
...
# open gnuplot process
gp = os.popen("/usr/bin/gnuplot, 'w')
# initialize the plot with desired settings including definition of
# "weekday" function.
...
# send plot command to gnuplot process
gp.write("""plot '-' u 1:(weekday(timecolumn(1)) ? column(2) : 0/0) ti
"Weekday" w impulses, \
'' u 1:(weekday(timecolumn(1)) ? column(2) : 0/0) ti "Weekend" w
impulses\n""")
# send data to the plot command
for record in result:
gp.write("%s %2.3f %d\n" % record)
gp.write("e\n")
#bombs out if following is uncommented.
#gp.close()
Again, I can't see anything here that would work if the gnuplot process
was left open but fail if it is closed.
Thomas Sefzick wrote:
> there should be information about what caused the error in
> the error-log of the http-server.
>
>
> stevecoh1 wrote:
>> Am trying to use gnuplot as a subprocess to a CGI script written in
>> python (similar to the perl script on p. 241 of "Gnuplot in Action").
>>
>> The python script itself follows the pattern of the python script on
>> p.230-231, that is
>>
>> gp = os.popen( ' /usr/bin/gnuplot', 'w')
>> ... plotting commands
>> gp.close()
>>
>> The cgi script produces a 500 error.
>>
>> But, if I comment out the gp.close() command, it works perfectly, and
>> produces the graph I want. What is going on here?
>>
>>
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Let Crystal Reports handle the reporting - Free Crystal Reports 2008
>> 30-Day
>> trial. Simplify your report design, integration and deployment - and focus
>> on
>> what you do best, core application coding. Discover what's new with
>> Crystal Reports now. http://p.sf.net/sfu/bobj-july
>> _______________________________________________
>> Gnuplot-info mailing list
>> Gnu...@li...
>> https://lists.sourceforge.net/lists/listinfo/gnuplot-info
>>
>>
>
|
|
From: Steve C. <sc...@ja...> - 2009-11-16 15:53:08
|
OK, I feel like an idiot but I guess I'm really not understanding you
even after reading the document you referred me to. The specific words
I'm not understanding are "going separate ways".
To that end I've gone back to basics and rewritten the example on p. 241
in python. I believe my translation is more or less exact:
================================
#!/usr/bin/python
import os
import math
print "Content-Type: image/png" # This is an image
print # blank line, end of headers
gp = os.popen("/usr/bin/gnuplot", 'w')
gp.write("set term png;set o;")
gp.write("plot '-' u 1:2 w linesp\n")
xs = [a/10.0 for a in range(0,101,1)]
for x in xs:
y = x * math.sin(x)
gp.write( "%f %f\n" % (x, y))
gp.write("e\n")
gp.close()
=================================
This fails with the following message
[error] [client ::1] malformed header from script. Bad header=\x89PNG:
whether or not the gp.close() line is commented out.
What am I missing?
Thomas Sefzick wrote:
> seems that your header:
>
> print "Content-Type: image/png" # This is an image
> print # blank line, end of headers
>
> and gnuplots output (png graphics) are going different
> ways, because the png graphics is sent without header
> (a png file starts with "\x89PNG").
>
> have a look at the section "Object Headers" in
> http://marc.merlins.org/htmlearn/misc/cgi-faq.txt
>
>
> stevecoh1 wrote:
>> Here's the error:
>>
>> [error] [client ::1] malformed header from script. Bad header=\x89PNG:
>> usage_graph.cgi
>>
...
|
|
From: Thomas S. <t.s...@fz...> - 2009-11-16 08:24:59
|
seems that your header: print "Content-Type: image/png" # This is an image print # blank line, end of headers and gnuplots output (png graphics) are going different ways, because the png graphics is sent without header (a png file starts with "\x89PNG"). have a look at the section "Object Headers" in http://marc.merlins.org/htmlearn/misc/cgi-faq.txt stevecoh1 wrote: > > Here's the error: > > [error] [client ::1] malformed header from script. Bad header=\x89PNG: > usage_graph.cgi > > I don't understand this. How could closing the gnuplot process create a > malformed header yet leaving it open produces usable output? Still, I > guess that's a clue. I just don't know what it means. > > Here is the guts of the script, deliberately leaving out details: > > #!/usr/bin/python > > import cgi > import MySQLdb > import os > > parms = cgi.FieldStorage() > > print "Content-Type: image/png" # This is an image > print # blank line, end of headers > > > # open database connection > ... > # create a cursor > cursor = db.cursor() > > # execute SQL query > cursor.execute(query % (userid)) > > # get the resultset as a tuple > result = cursor.fetchall() > ... > # open gnuplot process > gp = os.popen("/usr/bin/gnuplot, 'w') > > # initialize the plot with desired settings including definition of > # "weekday" function. > ... > > # send plot command to gnuplot process > gp.write("""plot '-' u 1:(weekday(timecolumn(1)) ? column(2) : 0/0) ti > "Weekday" w impulses, \ > '' u 1:(weekday(timecolumn(1)) ? column(2) : 0/0) ti "Weekend" w > impulses\n""") > > # send data to the plot command > for record in result: > gp.write("%s %2.3f %d\n" % record) > gp.write("e\n") > > #bombs out if following is uncommented. > #gp.close() > > Again, I can't see anything here that would work if the gnuplot process > was left open but fail if it is closed. > > > Thomas Sefzick wrote: > >> there should be information about what caused the error in >> the error-log of the http-server. >> >> >> stevecoh1 wrote: >>> Am trying to use gnuplot as a subprocess to a CGI script written in >>> python (similar to the perl script on p. 241 of "Gnuplot in Action"). >>> >>> The python script itself follows the pattern of the python script on >>> p.230-231, that is >>> >>> gp = os.popen( ' /usr/bin/gnuplot', 'w') >>> ... plotting commands >>> gp.close() >>> >>> The cgi script produces a 500 error. >>> >>> But, if I comment out the gp.close() command, it works perfectly, and >>> produces the graph I want. What is going on here? >>> >>> >>> >>> >>> >>> ------------------------------------------------------------------------------ >>> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 >>> 30-Day >>> trial. Simplify your report design, integration and deployment - and >>> focus >>> on >>> what you do best, core application coding. Discover what's new with >>> Crystal Reports now. http://p.sf.net/sfu/bobj-july >>> _______________________________________________ >>> Gnuplot-info mailing list >>> Gnu...@li... >>> https://lists.sourceforge.net/lists/listinfo/gnuplot-info >>> >>> >> > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and focus > on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > Gnuplot-info mailing list > Gnu...@li... > https://lists.sourceforge.net/lists/listinfo/gnuplot-info > > -- View this message in context: http://old.nabble.com/strange-behavior-on-calling-gnuplot-from-cgi-script-written-in-python-tp26356744p26368260.html Sent from the Gnuplot - User mailing list archive at Nabble.com. |
|
From: Thomas S. <t.s...@fz...> - 2009-11-16 16:08:35
|
you're right, there are no 'separate ways' (i first thought the
'Content-Type' lines would go somewhere else than the gnuplot
output, but that's not the case).
both, the print commands and gnuplots output go through stdout - but,
i just ran your little script and realized that the 'Content-Type' lines are
at the end.
you can reproduce this by running your script in a terminal and
piping the output into a file.
you need a 'flush' after 'print'ing the two lines:
#!/usr/bin/python
import os
import sys
import math
print "Content-Type: image/png" # This is an image
print # blank line, end of headers
sys.stdout.flush();
gp = os.popen("/usr/bin/gnuplot", 'w')
gp.write("set term png;set o;")
gp.write("plot '-' u 1:2 w linesp\n")
xs = [a/10.0 for a in range(0,101,1)]
for x in xs:
y = x * math.sin(x)
gp.write( "%f %f\n" % (x, y))
gp.write("e\n")
gp.close()
stevecoh1 wrote:
>
> OK, I feel like an idiot but I guess I'm really not understanding you
> even after reading the document you referred me to. The specific words
> I'm not understanding are "going separate ways".
>
> To that end I've gone back to basics and rewritten the example on p. 241
> in python. I believe my translation is more or less exact:
>
> ================================
> #!/usr/bin/python
>
> import os
> import math
>
> print "Content-Type: image/png" # This is an image
> print # blank line, end of headers
>
> gp = os.popen("/usr/bin/gnuplot", 'w')
> gp.write("set term png;set o;")
> gp.write("plot '-' u 1:2 w linesp\n")
>
> xs = [a/10.0 for a in range(0,101,1)]
> for x in xs:
> y = x * math.sin(x)
> gp.write( "%f %f\n" % (x, y))
> gp.write("e\n")
> gp.close()
>
>
>
> =================================
>
> This fails with the following message
> [error] [client ::1] malformed header from script. Bad header=\x89PNG:
>
> whether or not the gp.close() line is commented out.
>
> What am I missing?
>
>
>
>
>
> Thomas Sefzick wrote:
>> seems that your header:
>>
>> print "Content-Type: image/png" # This is an image
>> print # blank line, end of headers
>>
>> and gnuplots output (png graphics) are going different
>> ways, because the png graphics is sent without header
>> (a png file starts with "\x89PNG").
>>
>> have a look at the section "Object Headers" in
>> http://marc.merlins.org/htmlearn/misc/cgi-faq.txt
>>
>>
>> stevecoh1 wrote:
>>> Here's the error:
>>>
>>> [error] [client ::1] malformed header from script. Bad header=\x89PNG:
>>> usage_graph.cgi
>>>
>
> ...
>
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008
> 30-Day
> trial. Simplify your report design, integration and deployment - and focus
> on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now. http://p.sf.net/sfu/bobj-july
> _______________________________________________
> Gnuplot-info mailing list
> Gnu...@li...
> https://lists.sourceforge.net/lists/listinfo/gnuplot-info
>
>
--
View this message in context: http://old.nabble.com/strange-behavior-on-calling-gnuplot-from-cgi-script-written-in-python-tp26356744p26374508.html
Sent from the Gnuplot - User mailing list archive at Nabble.com.
|
|
From: Steve C. <sc...@ja...> - 2009-11-16 16:47:49
|
Thank you! That's the answer. Might want to tell Mr. Janert about this so they can print an errata. Or is the flush not necessary in Perl? Thomas Sefzick wrote: > > you need a 'flush' after 'print'ing the two lines: > > #!/usr/bin/python > > import os > import sys > import math > |
|
From: Steven M. <smi...@gm...> - 2009-11-17 21:53:03
|
Auto flush might be turned on in perl $|=1 I suspect that this is like python unbuffered.... Steve On Nov 16, 2009, at 8:47 AM, Steve Cohen wrote: > Thank you! That's the answer. Might want to tell Mr. Janert about > this > so they can print an errata. Or is the flush not necessary in Perl? > > > Thomas Sefzick wrote: > >> >> you need a 'flush' after 'print'ing the two lines: >> >> #!/usr/bin/python >> >> import os >> import sys >> import math >> > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > Gnuplot-info mailing list > Gnu...@li... > https://lists.sourceforge.net/lists/listinfo/gnuplot-info |
|
From: Thomas S. <t.s...@fz...> - 2009-11-17 19:42:52
|
i'm not sure if 'flush' is really needed for perl. python has the '-u' option to have stdin, stdout, and stderr unbuffered. perl doesn't have a similar option. so i think it's a python-specific problem. BTW, by using #!/usr/bin/python -u as the 1st line of your script, you need no 'flush()' stevecoh1 wrote: > > Thank you! That's the answer. Might want to tell Mr. Janert about this > so they can print an errata. Or is the flush not necessary in Perl? > > > Thomas Sefzick wrote: > >> >> you need a 'flush' after 'print'ing the two lines: >> >> #!/usr/bin/python >> >> import os >> import sys >> import math >> > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and focus > on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > Gnuplot-info mailing list > Gnu...@li... > https://lists.sourceforge.net/lists/listinfo/gnuplot-info > > -- View this message in context: http://old.nabble.com/strange-behavior-on-calling-gnuplot-from-cgi-script-written-in-python-tp26356744p26396244.html Sent from the Gnuplot - User mailing list archive at Nabble.com. |
|
From: Steve C. <sc...@ja...> - 2009-11-18 04:02:03
|
Thanks for that hint. I like learning something new every day. :-) Steve Thomas Sefzick wrote: > i'm not sure if 'flush' is really needed for perl. > > python has the '-u' option to have stdin, stdout, > and stderr unbuffered. > perl doesn't have a similar option. > so i think it's a python-specific problem. > > BTW, by using > #!/usr/bin/python -u > as the 1st line of your script, you need no 'flush()' > > > stevecoh1 wrote: >> Thank you! That's the answer. Might want to tell Mr. Janert about this >> so they can print an errata. Or is the flush not necessary in Perl? >> >> >> Thomas Sefzick wrote: >> >>> you need a 'flush' after 'print'ing the two lines: >>> >>> #!/usr/bin/python >>> >>> import os >>> import sys >>> import math >>> >> ------------------------------------------------------------------------------ >> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 >> 30-Day >> trial. Simplify your report design, integration and deployment - and focus >> on >> what you do best, core application coding. Discover what's new with >> Crystal Reports now. http://p.sf.net/sfu/bobj-july >> _______________________________________________ >> Gnuplot-info mailing list >> Gnu...@li... >> https://lists.sourceforge.net/lists/listinfo/gnuplot-info >> >> > |