开发者

Auto-login in python using mechanize

开发者 https://www.devze.com 2023-04-06 21:17 出处:网络
I have an almost working code to connect to a server and do a search. But there is some issue here with the code. This is what I do now with the following python script. I login to the site. Construct

I have an almost working code to connect to a server and do a search. But there is some issue here with the code. This is what I do now with the following python script. I login to the site. Construct a url for the search result and access the search page and open it in a browser. But issue I am facing here is that I am getting a session expired page. But if I have a logged in page open(manually) in the browser, this constructed url is giving me the desired output. So my question here is that How can I keep the 'logged in' session in the script active and open the constructed url in a browser which yield me the desired output.

#!/usr/bin/env python

import urllib, urllib2, cookielib, mechanize, webbrowser, subprocess
from mechanize import ParseResponse, urlopen, urljoin

def main():
    usr = 'sg092350'
    pwd = 'gk530911'
    login_url = 'http://int15.sla.findhere.net/logininq.act?&site=superarms'

    search_url_1 = '&need_air=yes&need_rail=no&need_train=no&need_hotel=no&need_car=no&origin_request=regular+booking&monAb开发者_运维技巧brList[0]=9&monAbbrList[1]=9&dateList[0]=29&dateList[1]=30&pickUpCity=&pickUpTime=&dropOffCity=&dropOffTime=&dispAOptions=&dispADestinations=&checkSurroundingAirports=false&doEncodeDecodeForSurrArpt=false&checkPlusMinusDays=N&tripType=roundTrip&itinType=on&departList[0]=BLR&destinationList[0]=DEL&date0=9%2F29%2F11&travelMethodList[0]=departs&timeList[0]=8&date1=9%2F30%2F11&travelMethodList[1]=departs&timeList[1]=8&numPassengers=1&cabinClass=1&pricingType=1&preferredCarrier[0]=&preferredCarrier[1]=&preferredCarrier[2]=&userRequestedWebFares=true'



    br = mechanize.Browser()
    cj = cookielib.CookieJar()

    br.set_cookiejar(cj)
    br.set_handle_robots(False)

    br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2')]

    br.open(login_url)
    br.select_form('loginForm')

    br.form['user'] = usr
    br.form['pwd'] = pwd

    br.submit()
    print br.geturl()

    response = urlopen(login_url)
    forms = ParseResponse(response, backwards_compat=False)
    form = forms[0]
    token = forms[0]['token']
    site_id = forms[0]['siteID']
    site = forms[0]['site']
    water_mark = forms[0]['watermark']
    trans_index = forms[0]['transIndex']


    search_url_0 = 'http://int15.sla.findhere.net/pwairavail.act;' + '?site=' + site + '&sid=4' + '&siteID=' + site_id + '&watermark=' + water_mark + '&token=' + token + '&transIndex=' + trans_index + search_url_1
    print search_url_0

    print token
    print site_id
    print site
    print water_mark
    print trans_index

    print form

    response.read()

    #Inserting code to generate html and display the overlay

    htmlString = """
    <html>
    <head>
    <title>DirectBook 1.0</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <script type="text/javascript" src="./fx/jquery.fancybox-1.3.4.pack.js"></script>
    <link rel="stylesheet" type="text/css" href="./fx/jquery.fancybox-1.3.4.css" media="screen"/>
    <script type="text/javascript">
    $(document).ready(function() {
    $("#urlLink").fancybox
    ({
    'width'      : '100%',
    'height'      : '100%',
    'autoScale'      : false,
    'transitionIn'   : 'fade',
    'transitionOut'   : 'fade',
    'type'      : 'iframe'
    });
    });
    </script>
    </head>
    <body onload="document.getElementById('urlLink').click()">
    <div id="content">
    <script type="text/javascript">
    var search_url = " """ + search_url_0 + """ ";
    document.write('<a id="urlLink"' + 'href="' + search_url + '"></a>');
    </script>
    </div>
    </body>
    </html>"""


    # write the html file to the working folder
    fout = open("search.html", "w")
    fout.write(htmlString)
    fout.close()

    subprocess.Popen('"C:\\Program Files\\Mozilla Firefox\\firefox.exe" "C:\\Python27\\mechanize-0.2.5\\search.html"')


if __name__ == '__main__':
    main()


You need to share the session (or session identifier, which is probably stored in a cookie) between Mechanize and your browser. This is not easy, and definitely not portable between browsers (if you want that).

However, there appears to be support in Mechanize for the SQLite database format that Firefox uses since version 3: https://github.com/jjlee/mechanize/blob/master/mechanize/_firefox3cookiejar.py

You may want to check the documentation for that.


This might be helpful for the login part:

(Was able to get site to send data using wireshark. Also "user" might be something else e.g. "username" same with "password". Once again wireshark would help with this. Also could look at source of login page. Good luck!!!)

from urllib import urlencode

from urllib2 import Request, urlopen

req = Request('www.site.com',urlencode({'user':'userhere', 'password':'passwordhere'}))

open = urlopen(req)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号