LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2005, 07:52 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
Cookies when fetching site with perl


I am writing a script in Perl that uses LWP::UserAgent to fetch a website. But the problem is that the site has a loginsystem based on cookies. So I want to use all the cookies I have in Firefox that is for that site and use them when I fetch the page with my script.

Can this be done?

I looked into cookie_jar but I wasn't sure how to set a couple of cookies. Any help?

Thanks.
 
Old 07-12-2005, 08:31 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Firefox seems to store cookies in a plain text file in ~/.mozilla/firefox/<random-dir>/cookies.txt that you can parse. But there seems to be a HTTP::Cookies::Netscape Perl module that will parse a Netscape compatible cookie file and load it into a cookie jar. I'm not sure if the Firefox cookie file is compatible, but it might be worth looking into. FYI, you can find more info on the HTTP::Cookie Perl module here.
 
Old 07-13-2005, 02:31 AM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Thanks, I will check into it ASAP.
 
Old 07-14-2005, 03:11 PM   #4
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
OK, I have tried a bit but no success. I put up a temporary webpage with this code:
Code:
http://dreamland4.gotdns.org/~ephracis/cookies.php:
<?
echo "Foo: < " . $_COOKIE['foo'] . " ><br>\n";
echo "UA : < " . $_SERVER['HTTP_USER_AGENT'] . " ><br>";
?>
I visit the page with Firefox and it looks like:
Quote:
Foo: < >
UA : < Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8b2) Gecko/20050524 Firefox/1.0+ >
I have this perl program:
Code:
#!/usr/bin/perl
require LWP::UserAgent;
$ua = new LWP::UserAgent(agent => 'Mozilla/4.0 ()', 'cookie_jar' => {file => "/home/ephracis/programming/perl/cookies.txt"} );
$request = new HTTP::Request('GET', 'http://dreamland4.gotdns.org/~ephracis/cookies.php');
$response = $ua->request($request, \&callback, 4096);

sub callback {
        my($data, $response, $protocol) = @_;
        print "$data \n";
}
It uses this cookie-file:
Code:
#LWP-Cookies-1.0
Set-Cookie3: foor=bar; path="~ephracis"; domain="dreamland4.gotdns.org"; path_spec; expires="2005-07-20 10:10:10Z"; version=0
I run the perl program and get this output:
Code:
$ ./fetchcookies.pl
Foo: <  ><br>
UA : < libwww-perl/5.53 ><br>
Am I missing something here? Shouldn't at least the user agent change?

Last edited by Ephracis; 07-14-2005 at 03:12 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fetchmail not fetching???? blizunt7 Linux - General 0 08-25-2005 12:05 PM
perl -MCPAN -e shell # PROBLEM FINDING CPAN site Louis_Carole Linux - Software 1 08-26-2004 05:55 PM
c++ web fetching lrt2003 Programming 3 06-21-2004 11:43 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
fetchmail not fetching marlaina1 Linux - General 1 11-13-2001 11:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration