Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Howto fill in username and password in a popup cookie with Perl

by monkfan (Curate)
on Sep 22, 2006 at 09:59 UTC ( [id://574350]=perlquestion: print w/replies, xml ) Need Help??

monkfan has asked for the wisdom of the Perl Monks concerning the following question:

I have a website which is located in the local server. Now, with browser it generally gives a prompt in form of popup for us to fill in user name and password.

Following Corion's advice. I tried to mimic the process with this following Perl script, especially with WWW::Mechanize credentials method.
use WWW::Mechanize; my $addr = 'http://myhost.com.sg/cgi-bin/biobase/transfac/9.4/bin/star +t.cgi'; my $username = 'myname'; my $pwd = 'somepassword'; my $mech = WWW::Mechanize->new(); $mech->get($addr); $mech->credentials($username,$pwd); my $result = $mech->content(); print "$result\n";
But it gives:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Authorization Required</title> </head><body> <h1>Authorization Required</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.</p> <hr /> <address>Apache/2.0.46 (Red Hat) Server at myhost.com.sg Port 80</addr +ess> </body></html>
Did I use the right module and its method? How can I know what kind of cookie they are using?

So that I can know which is the right module and method to use? Because I can't seem to figure out the cookie setting
by looking at the source code of that link.

Regards,
Edward

Replies are listed 'Best First'.
Re: Howto fill in username and password in a popup cookie with Perl
by shmem (Chancellor) on Sep 22, 2006 at 10:27 UTC
    Try swapping two lines:
    $mech->credentials($username,$pwd); $mech->get($addr);

    So you $mech object has the credentials before retrieving the page.

    update: s/options/object/. silly mistake..

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      shmem, thanks for the reply.

      But the code still fail to login (i.e same old output) after swapping those two lines as you suggested.

      Regards,
      Edward
Re: Howto fill in username and password in a popup cookie with Perl
by hgolden (Pilgrim) on Sep 22, 2006 at 14:44 UTC
    Hey

    I have a few thoughts. One is to enable cookies for Mech. That goes something like this:

    $mech->cookie_jar(HTTP::Cookies->new( file =>"mechcookies.txt", autosave => 1));
    If that and swapping those two lines isn't working, I do have one idea. I had a site I was scraping recently (with their permission), and I was having trouble getting Mech to log in. What eventually worked with was this:
    my @args = (Authorization => "Basic " .MIME::Base64::encode('USER:PASS +')); $mech->get('https://www.BlahBlah.com/secure-bin/Blahclub/login_redirec +t.cgi', @args); $mech->get('http://www.BlahBlah.com/ThingIWant')
    I think part of the trick was making sure that I was sending that username/password combination to the login-script. Though for all I know, once I was calling the get on that url, it was the credentials statement that made it work (logging-in with Mech is pretty new to me).

    If you're having trouble locating the appropriate url for the login, I've had a lot of luck with Live HTTP Headers for Firefox, which basically tracks everything your browser does (when it's on), and has helped me mimic that with Mech. Good luck!

    Hays

    Update: Cookies may be on by default in Mech, but having the hard-copy lets you see what's there (if anything).

      Hello,
      I stumbled across this node when i was trying to tackle the same kind of problem as the original poster.

      My Question is why did you need to add MIME::Base64 into the mix?. Granted it worked for me but i just don't understand why I had to do it instead of using the credentials method supplied with mech.

      Thanks for the help!

Re: Howto fill in username and password in a popup cookie with Perl
by bobf (Monsignor) on Sep 22, 2006 at 17:24 UTC

    my $addr = 'http://myhost.com.sg/cgi-bin/biobase/transfac/9.4/bin/star +t.cgi';

    It looks like you're trying to access the transfac database. Unless something has changed since the last time I used it, the database is simply a hierarchy of easily-parsable flat files. If you're trying to use their web pages to do some searching, I would encourage you to consider accessing the data directly (in fact, if you look at the code in the cgi script you can see exactly how they locate and parse each file). If you use the data files it will likely be much quicker to program and easier to maintain. Moreover, the TFBS modules already provide some support for transfac.

    On a side note, please help us help you. I've noticed that many of your questions focus only on what appears to be the very next step in the process. It's perfectly acceptable to ask about how to use WWW::Mech and cookies to access a web site, but a more complete description of the problem might help us to provide you with better approaches to the overall problem rather than to just the next 10 lines of code in your program. Just 1 or 2 sentences explaining your goal (for example, "I have several multiple sequence alignments and I need to create a matrix from them, then I need to search transfac to see if that matrix is similar to any other known TF binding sites") might be enough for someone to mention a tool or two that already exist and that do exactly what you want.

    HTH

      bobf,

      Thank you so much for your pointer to TFBS module link. Very useful indeed. We don't even need password to access the TRANSFAC database! I wonder why this module isn't in CPAN.

      Actually the task I intend to do is as follows:
      1. Extract all the transcription factors (TFBS) for fruitfly (D. Melanogaster).
      2. Identify all the coregulated genes of each TFBS found above.
      3. Finally, extract the the sequence from -450 upstream to 50 downstream region of each coregulated genes found.
      Do you have any experience doing this similar kind of thing with this module? Perhaps you can advice how can this be done.

      Regards,
      Edward

        You're welcome. Not all modules are on CPAN, and in those cases Google is your friend. :-)

        Thank you for the brief description of your project. Unfortunately, a complete analysis of TF binding sites and coregulated genes is very difficult if not impossible (at the current moment in time), even in a model organism like Drosophila. Therefore, keep in mind that any results you obtain are going to be incomplete.

        1. Extract all the transcription factors (TFBS) for fruitfly (D. Melanogaster).
        Until all of the TFs in Drosophila have been characterized, they aren't going to be in transfac (or any other db). It is trivial to grab TF records from transfac that have some data from Drosophila, though, so you can at least get data for those TFs that have been characterized thus far. Go forth and parse (the TFBS modules may help you here).

        2. Identify all the coregulated genes of each TFBS found above.
        If I understand you correctly, you want to identify the genes that are regulated by each TF. That's a much harder problem, and it has been a subject of active research for many years. I encourage you to do some searches on PubMed for background and talk to someone at your institution that might have experience in the area. This step will likely require some experimental data (e.g., expression microarrays).

        It is certainly possible to identify genes that contain sequences that match a given matrix, but that does not mean that the TF actually binds to that site and regulates the gene in question. Take a look at the PATSER program (Hertz and Stormo, Bioinformatics 1999). It's even part of bioperl: Bio::Tools::Run::PiseApplication::patser. :-)

        3. Finally, extract the the sequence from -450 upstream to 50 downstream region of each coregulated genes found.
        Once you identify the genes, extracting a portion of the sequence is trivial. Bioperl to the rescue (keep in mind that many TF binding sites could be located outside of your defined region).

        HTH

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://574350]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-19 04:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found