help regarding CGI programming

Reply

Join Date: Feb 2008
Posts: 14
Reputation: dilbert_here00 is an unknown quantity at this point 
Solved Threads: 1
dilbert_here00 dilbert_here00 is offline Offline
Newbie Poster

help regarding CGI programming

 
0
  #1
Feb 6th, 2008
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
  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:

  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 31
Reputation: Passmark is an unknown quantity at this point 
Solved Threads: 0
Passmark's Avatar
Passmark Passmark is offline Offline
Light Poster

Re: help regarding CGI programming

 
0
  #2
Feb 6th, 2008
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.
  1. <html><head></head>
  2. <body>
  3. <a href="resume.pdf">Resume(.pdf 25kb)</a>
  4. </body></html>
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 14
Reputation: dilbert_here00 is an unknown quantity at this point 
Solved Threads: 1
dilbert_here00 dilbert_here00 is offline Offline
Newbie Poster

Re: help regarding CGI programming

 
0
  #3
Feb 7th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: anurag_bagaria is an unknown quantity at this point 
Solved Threads: 0
anurag_bagaria anurag_bagaria is offline Offline
Newbie Poster

Re: help regarding CGI programming

 
0
  #4
Dec 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,017
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 286
woooee woooee is offline Offline
Veteran Poster

Re: help regarding CGI programming

 
0
  #5
Dec 11th, 2008
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')
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 14
Reputation: dilbert_here00 is an unknown quantity at this point 
Solved Threads: 1
dilbert_here00 dilbert_here00 is offline Offline
Newbie Poster

Re: help regarding CGI programming

 
0
  #6
Dec 11th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,017
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 286
woooee woooee is offline Offline
Veteran Poster

Re: help regarding CGI programming

 
0
  #7
Dec 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: help regarding CGI programming

 
0
  #8
Dec 14th, 2008
Originally Posted by woooee View Post
file_path = os.path.join("c:", "Broucher.pdf")
For whatever reason, on Windows os.path.join doesn't play nice with the drive letter:
  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. >>>
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: anurag_bagaria is an unknown quantity at this point 
Solved Threads: 0
anurag_bagaria anurag_bagaria is offline Offline
Newbie Poster

Re: help regarding CGI programming

 
0
  #9
Dec 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC