Python - Mechanize sessions are not regonized

自作多情 提交于 2020-01-02 08:57:12

问题


I'm behind a proxy in a company. And in order to access some intra-sites, I have to login first to a specific site. The thing is if I login to this specific site from IE or FF, then I can access the intra-sites not necessarily from the same browser where I logged in.

For ex: I logged in from FF, then I can access the intra-sites from IE and vice versa (and I get ping reply from the intra-sites).

But, when I login using mechanize and while keeping the session alive (by using sleep), I can not access any of the intra-sites (and I get ping timeout)

Why are mechanize session not recognized?

Update: My Code

import mechanize
import cookielib
from time import sleep

loginURL = '<loginURL>'
URL = '<URL>'

browser = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
browser.set_cookiejar(cj)

#Browser Options
browser.set_handle_robots(False)
browser.set_handle_equiv(True)
browser.set_handle_gzip(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
#User-agent
browser.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:43.0) Gecko/20100101 Debian 3.2.73-2+deb7u1 Firefox/43.0')]
browser.open(loginURL)
browser.select_form(nr = 0)
browser.form['username'] = '<username>'
browser.form['password'] = '<password>'
login = browser.submit()
#Checking Successful login
if browser.geturl()== URL:
    print 'Successful Login'
print 'keeping session...'
sleep(360)

#Checking that session was kept alive
open = browser.open(URL)
sleep (5)
if browser.geturl()== URL:
    print 'Session Kept Alive!'
#print open.read()

来源:https://stackoverflow.com/questions/34993645/python-mechanize-sessions-are-not-regonized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!