| | |
help regarding CGI programming
![]() |
•
•
Join Date: Feb 2008
Posts: 14
Reputation:
Solved Threads: 1
Hi,
I am learning python CGI programming but got stuck with some issues. I would really appreciate if you could help me out.
Problem: I have made a web page using Python CGI scripts; from one of the menu items on the web page, I am trying to call another python cgi script which opens a pdf file. Following is the code:
In index.py
In resume.py:
When I execute http://localhost/cgi-bin/index.py the web page is displayed with 'resume' as the menu item.
When i click on menu 'resume' the script resume.py is executed but the pdf file is not opening. 'Done' is reflected on the bottom left corner of the browser & a white page is displayed.
I also noticed that when I click the 'resume' menu item at the same time a 'AcroRd32.exe' process starts in the system processes but no pdf file is displayed nor any error is thrown.
Further when I execute the script resume.py from the command line the pdf file opens up but not from browser.
Could you please throw some light on the issue?
Thanks & Regards
I am learning python CGI programming but got stuck with some issues. I would really appreciate if you could help me out.
Problem: I have made a web page using Python CGI scripts; from one of the menu items on the web page, I am trying to call another python cgi script which opens a pdf file. Following is the code:
In index.py
Python Syntax (Toggle Plain Text)
#!C:\Python25\python.exe import cgi import cgitb; cgitb.enable() print "Content-type: text/html\n" <html><head> <title>My page</title> <link rel="stylesheet" href="C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\mystyle.css"/></head> <body><ul class="glossymenu"><li class="current"> <a href="resume.py"><b>Resume(.pdf 25kb)</b></a></li></ul></body></html>
Python Syntax (Toggle Plain Text)
#!C:\Python25\python.exe import cgi,os import cgitb cgitb.enable() print "Content-type: text/html\n" os.system('c:\resume.pdf')
When I execute http://localhost/cgi-bin/index.py the web page is displayed with 'resume' as the menu item.
When i click on menu 'resume' the script resume.py is executed but the pdf file is not opening. 'Done' is reflected on the bottom left corner of the browser & a white page is displayed.
I also noticed that when I click the 'resume' menu item at the same time a 'AcroRd32.exe' process starts in the system processes but no pdf file is displayed nor any error is thrown.
Further when I execute the script resume.py from the command line the pdf file opens up but not from browser.
Could you please throw some light on the issue?
Thanks & Regards
I have never written a line of Python code in my life. So I am superbly qualified to answer this post.
But I am guessing the os.system call just calls the operating system to execute the file supplied as the argument. So it won't return anything to the browser.
But you don't need Python for this. You can replace all this with the following HTML code.
But I am guessing the os.system call just calls the operating system to execute the file supplied as the argument. So it won't return anything to the browser.
But you don't need Python for this. You can replace all this with the following HTML code.
Python Syntax (Toggle Plain Text)
<html><head></head> <body> <a href="resume.pdf">Resume(.pdf 25kb)</a> </body></html>
•
•
Join Date: Feb 2008
Posts: 14
Reputation:
Solved Threads: 1
Hi
Thanks for your reply!!
I am trying it with python just to get my hands on with pyhton CGI.
Regarding the os.system call: The pdf file opens up when i execute the resume.py from command line or double click it.So I dont think that should be the issue.
I had tried a similar approach as well using HTML code but the error I get is :
[error] [client 127.0.0.1] (9)Bad file descriptor: don't know how to spawn child process: C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/resume.pdf, referer: http://localhost/cgi-bin/index.py
Thanks
Thanks for your reply!!
I am trying it with python just to get my hands on with pyhton CGI.
Regarding the os.system call: The pdf file opens up when i execute the resume.py from command line or double click it.So I dont think that should be the issue.
I had tried a similar approach as well using HTML code but the error I get is :
[error] [client 127.0.0.1] (9)Bad file descriptor: don't know how to spawn child process: C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/resume.pdf, referer: http://localhost/cgi-bin/index.py
Thanks
Last edited by dilbert_here00; Feb 7th, 2008 at 12:38 am. Reason: added an extra line
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
Hello,
I was just curious if you could finally get the solution to this problem which you have referred to as I too have just started to get my hands over Python CGI programming.
I guess, being so long, by now you might have overcome this problem.
Looking forward to a positive response and thanking you in advance.
I was just curious if you could finally get the solution to this problem which you have referred to as I too have just started to get my hands over Python CGI programming.
I guess, being so long, by now you might have overcome this problem.
Looking forward to a positive response and thanking you in advance.
•
•
Join Date: Feb 2008
Posts: 14
Reputation:
Solved Threads: 1
Hi,
It still dint work
Now iv started feeling that its a windows problem.
This is what the latest version of my script looks like:
[code]
#! C:\Python25\python.exe
import os.path, cgi
import cgitb
cgitb.enable()
file_path = "c:/Broucher.pdf"
print "Content-Type: application/pdf"
print "Content-Transfer-Encoding: binary"
#print "Content-Length: %s" % (os.path.getsize(file_path) + os.path.getsize(file_path))
#print "Content-Disposition: attachment; filename=%s" % os.path.split(file_path)[1]
print
print open(file_path).read(),
[\code]
When I run this script it says PDF file is corrupt. Although the file is perfectly fine.
Do let me know if you find out a solution.
Thanks
It still dint work
Now iv started feeling that its a windows problem.This is what the latest version of my script looks like:
[code]
#! C:\Python25\python.exe
import os.path, cgi
import cgitb
cgitb.enable()
file_path = "c:/Broucher.pdf"
print "Content-Type: application/pdf"
print "Content-Transfer-Encoding: binary"
#print "Content-Length: %s" % (os.path.getsize(file_path) + os.path.getsize(file_path))
#print "Content-Disposition: attachment; filename=%s" % os.path.split(file_path)[1]
print open(file_path).read(),
[\code]
When I run this script it says PDF file is corrupt. Although the file is perfectly fine.
Do let me know if you find out a solution.
Thanks
•
•
Join Date: Dec 2006
Posts: 1,017
Reputation:
Solved Threads: 286
To open it, as opposed to viewing in Acrobat, you will likely have to open as a binary file
print open(file_path, "rb")
There is pypdf, and probably others, for working with a pdf file in python. You would also probably use PIL (Python Imaging Library) if there are embedded images in the pdf file.
You may have to use a double backslash, as I'm on Linux I don't know, so just use .join if there are problems.
file_path = os.path.join("c:", "Broucher.pdf")
And are you sure it is not "Brochure"? HTH.
print open(file_path, "rb")
There is pypdf, and probably others, for working with a pdf file in python. You would also probably use PIL (Python Imaging Library) if there are embedded images in the pdf file.
You may have to use a double backslash, as I'm on Linux I don't know, so just use .join if there are problems.
file_path = os.path.join("c:", "Broucher.pdf")
And are you sure it is not "Brochure"? HTH.
Last edited by woooee; Dec 11th, 2008 at 10:50 pm.
For whatever reason, on Windows os.path.join doesn't play nice with the drive letter:
python Syntax (Toggle Plain Text)
>>> import os >>> os.path.join('c:', 'foo', 'bar') 'c:foo\\bar' >>> os.path.join('c:\\', 'foo', 'bar') 'c:\\foo\\bar' >>> os.path.join(r'c:', 'foo', 'bar') 'c:foo\\bar' >>>
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
I too am trying to write a similar program that I would be using later to integrate with CGI, so that the work that is done by the script through a command prompt can be done directly through the browser, just by the click of a button (or similar action).
I have some sort of bug in my initial script itself, which gives me the following error. The Script and the error are as follows.
SCRIPT-->
#!/usr/bin/python
import cgi, string, os, sys, cgitb, commands, subprocess
import posixpath, macpath
comd = [\
"tar -xf x.tar.gz", \
"cd demo", \
"cp README ../", \
]
outFile = os.path.join(os.curdir, "output.log")
outptr = file(outFile, "w")
errFile = os.path.join(os.curdir, "error.log")
errptr = file(errFile, "w")
retval = subprocess.call(comd, 0, None, None, outptr, errptr)
errptr.close()
outptr.close()
if not retval == 0:
errptr = file(errFile, "r")
errData = errptr.read()
errptr.close()
raise Exception("Error executing command: " + repr(errData))
ERROR-->
Traceback (most recent call last):
File "process.py", line 18, in <module>
retval = subprocess.call(comd, 0, None, None, outptr, errptr)
File "/usr/lib/python2.5/subprocess.py", line 443, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.5/subprocess.py", line 593, in __init__
errread, errwrite)
File "/usr/lib/python2.5/subprocess.py", line 1135, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I would be thankful to you all for any suggestions in this regard and also for some precautions that I may require to get this working via the browser too.
Regards.
I have some sort of bug in my initial script itself, which gives me the following error. The Script and the error are as follows.
SCRIPT-->
#!/usr/bin/python
import cgi, string, os, sys, cgitb, commands, subprocess
import posixpath, macpath
comd = [\
"tar -xf x.tar.gz", \
"cd demo", \
"cp README ../", \
]
outFile = os.path.join(os.curdir, "output.log")
outptr = file(outFile, "w")
errFile = os.path.join(os.curdir, "error.log")
errptr = file(errFile, "w")
retval = subprocess.call(comd, 0, None, None, outptr, errptr)
errptr.close()
outptr.close()
if not retval == 0:
errptr = file(errFile, "r")
errData = errptr.read()
errptr.close()
raise Exception("Error executing command: " + repr(errData))
ERROR-->
Traceback (most recent call last):
File "process.py", line 18, in <module>
retval = subprocess.call(comd, 0, None, None, outptr, errptr)
File "/usr/lib/python2.5/subprocess.py", line 443, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.5/subprocess.py", line 593, in __init__
errread, errwrite)
File "/usr/lib/python2.5/subprocess.py", line 1135, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I would be thankful to you all for any suggestions in this regard and also for some precautions that I may require to get this working via the browser too.
Regards.
![]() |
Similar Threads
- Programming FAQ - Updated 1/March/2005 (Computer Science)
- CGI + custom error pages... (Perl)
- Perl/cgi programmer is needed (Perl)
- how to Upload files using Linux bash Shell scripting and CGI (Shell Scripting)
- Compiling Programs (C++)
- cgi (HTML and CSS)
Other Threads in the Python Forum
- Previous Thread: Simplest way to change colour
- Next Thread: Python os.system() help (Newbie)
| Thread Tools | Search this Thread |
address alarm anydbm app beginner cipher conversion coordinates curves cx-freeze data development dictionary directory dynamic examples excel feet file float format function generator getvalue gui halp handling homework images import input ip itunes java keycontrol line linux list lists loan loop maintain maze millimeter mouse mysqldb number numbers output parsing path port prime programming projects py2exe pygame pyglet pymailer python queue random recursion recursive screensaverloopinactive script scrolledtext searchingfile shebang slicenotation split ssh string strings table terminal text thread threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wx.wizard wxpython xlwt






