943,871 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2455
  • Python RSS
Feb 6th, 2008
0

help regarding CGI programming

Expand Post »
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
Python Syntax (Toggle Plain Text)
  1. #!C:\Python25\python.exe
  2. import cgi
  3. import cgitb; cgitb.enable()
  4. print "Content-type: text/html\n"
  5. <html><head> <title>My page</title>
  6. <link rel="stylesheet" href="C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\mystyle.css"/></head>
  7. <body><ul class="glossymenu"><li class="current">
  8. <a href="resume.py"><b>Resume(.pdf 25kb)</b></a></li></ul></body></html>
In resume.py:

Python Syntax (Toggle Plain Text)
  1. #!C:\Python25\python.exe
  2. import cgi,os
  3. import cgitb
  4. cgitb.enable()
  5. print "Content-type: text/html\n"
  6. 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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
dilbert_here00 is offline Offline
25 posts
since Feb 2008
Feb 6th, 2008
0

Re: help regarding CGI programming

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.
Python Syntax (Toggle Plain Text)
  1. <html><head></head>
  2. <body>
  3. <a href="resume.pdf">Resume(.pdf 25kb)</a>
  4. </body></html>
Reputation Points: 36
Solved Threads: 0
Light Poster
Passmark is offline Offline
32 posts
since Jan 2008
Feb 7th, 2008
0

Re: help regarding CGI programming

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
Last edited by dilbert_here00; Feb 7th, 2008 at 12:38 am. Reason: added an extra line
Reputation Points: 10
Solved Threads: 1
Light Poster
dilbert_here00 is offline Offline
25 posts
since Feb 2008
Dec 11th, 2008
0

Re: help regarding CGI programming

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anurag_bagaria is offline Offline
3 posts
since Dec 2008
Dec 11th, 2008
0

Re: help regarding CGI programming

Just an educated guess, but you would have os.system call the pdf browser with the file name (but this is on Linux).
os.system('Acrobat c:\resume.pdf')
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Dec 11th, 2008
0

Re: help regarding CGI programming

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
Reputation Points: 10
Solved Threads: 1
Light Poster
dilbert_here00 is offline Offline
25 posts
since Feb 2008
Dec 11th, 2008
0

Re: help regarding CGI programming

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.
Last edited by woooee; Dec 11th, 2008 at 10:50 pm.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Dec 14th, 2008
0

Re: help regarding CGI programming

Click to Expand / Collapse  Quote originally posted by woooee ...
file_path = os.path.join("c:", "Broucher.pdf")
For whatever reason, on Windows os.path.join doesn't play nice with the drive letter:
python Syntax (Toggle Plain Text)
  1. >>> import os
  2. >>> os.path.join('c:', 'foo', 'bar')
  3. 'c:foo\\bar'
  4. >>> os.path.join('c:\\', 'foo', 'bar')
  5. 'c:\\foo\\bar'
  6. >>> os.path.join(r'c:', 'foo', 'bar')
  7. 'c:foo\\bar'
  8. >>>
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Dec 23rd, 2008
0

Re: help regarding CGI programming

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anurag_bagaria is offline Offline
3 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Simplest way to change colour
Next Thread in Python Forum Timeline: Python os.system() help (Newbie)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC