Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LWP::Simple script 1

Status
Not open for further replies.

yahve

Programmer
Nov 14, 2000
159
0
0
CA
I have a script that works OK from the command line but bombs from the web server:

#!/usr/bin/perl
use LWP::Simple;
$url = '$content = get($url);
print "$content";

Here is the server error_log:

httpd: [Wed Nov 15 00:11:52 2000] [error] [client xxx.xxx.xxx.xxx] malformed header from script. Bad header=<HTML><BODY>: /home/httpd/cgi-bin/script.pl

Can someone please help.
Thanks.
 
You need to add a 'Content-type' line to make the output compliant so the browser understands what it is getting.

Code:
#!/usr/bin/perl
use LWP::Simple;
$url = '[URL unfurl="true"]http://whatever/labo.asp';[/URL]
$content = get($url);
print &quot;Content-type: text/html\n\n&quot;;
print &quot;$content&quot;;

When you print to a browser from a piece of CGI, you must provide the Content-type info. When you use the get method to retrieve a remote page, you do not get that line with the remote page. So, you must add it before you try to print the retrieved page back to your browser.

'hope this helps.....




keep the rudder amid ship and beware the odd typo
 
Thanks for your input goBoating.
So I have changed the script like you said, and now, when I access the script, I get a download dialog box. If I save the file to disk and look at it, I find it contains the output from the script, with the html and head and other tags. Why doesn't it just open in my browser???
 
If copied the text of the code in my previous post, changed the first line to point at my Perl, changed the 'whatever/labo.asp' to a real page, and ran it and it worked fine.
You must have STDOUT redirected or a different file handle SELECTed, if your output is not making it to the browser.

I would suggest playing with the code above until you can make it work. Don't put it in anything else, just get it to work by itself. Once, your convinced it works, try to fold it into your other code.

'hope this helps....




keep the rudder amid ship and beware the odd typo
 
ok now it works... my mistake: I had
print &quot;Content-type: text\html\n\n&quot;;
instead of
Code:
print &quot;Content-type: text/html\n\n&quot;;
thanks.
 
Hi i am a beginner in perl. I tried to use the script you mentioned above. But i don't seem to be able to run the script. I am suspecting that i am behind the firewall. Have you any ideas of how to get around that?
 
hey coke,

The firewall is not my first guess for your problem. Your firewall should allow responses to internal requests to come back into your network. If you can reach the web with a browser from inside your fire wall, then the firewall is not your problem. The Perl LWP::Simple module behaves just like a browser request for a web page.

Are you sure the script runs?
While in the dir where the script is sitting, try 'perl -c scriptName', where scriptName is the name you saved it to. That will do a syntax check on the code.

If that works, try 'perl -e scriptName SomeWebAddress'. That should retrieve the requested HTML and blow it all over the screen (raw HTML).

Let me know if you need more.

'hope this helps.




keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top