Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 611459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:47:11+00:00 2026-05-13T17:47:11+00:00

I am using urllib2 and HTTPCookieProcessor to login to a website. I want to

  • 0

I am using urllib2 and HTTPCookieProcessor to login to a website. I want to login to multiple accounts concurrently and store the cookies to be reused later.

Can you recommend an approach or library to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:47:11+00:00Added an answer on May 13, 2026 at 5:47 pm

    The OP clarified that this is not a concurrency issue. With sequential processing in mind, this is much simpler. I once used something like the following to update a bunch of SIP phone base stations (they had a web front-end which you could use to upload VCard files for the phone book). Note that I just cut away some crap and renamed this and that in this hacky script, I did not test it at all. Its sole purpose is to give the OP an idea on how he could deal with this.

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    from optparse import OptionParser
    import sys
    from mechanize import Browser, CookieJar, Request, urlopen
    
    
    accounts = [
        {'ipaddr': '127.0.0.1', 'user': 'joe', 'pass': 'foobar'},
        ]
    
    
    class WebsiteAccount(object):
    
        def __init__(self, ipaddr, username, password, browser):
            self.ipaddr = ipaddr
            self.username = username
            self.password = password
            self.browser = browser
            self.cookiejar = CookieJar()
            self.browser.set_cookiejar(self.cookiejar)
    
        def login(self):
            self.browser.open('http://'+self.ipaddr+'/login.html')
            self.browser.select_form(name='loginform')
            self.browser.form.set_value(self.username, name='username')
            self.browser.form.set_value(self.password, name='password')
            resp = self.browser.submit()
            print 'Logging into account %s@%s ...' % (self.username, self.ipaddr),
            if resp.geturl().endswith('/login.html'):
                print 'FAILED!'
                sys.exit(1)
            print ' OK'
    
        def logout(self):
            print ('Logging out from account %s@%s...' % (self.username, self.ipaddr),
            self.browser.open('http://'+self.ipaddr+'/logout.html')
            self.browser.close()
            print 'OK'
    
    
    def main():
        parser = OptionParser()
        parser.add_option('-d', '--debug', action='store_true', dest='debug', default=False)
        parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False)
        (opts, args) = parser.parse_args()
        for account in accounts:
            browser = Browser()
            browser.set_handle_referer(True)
            browser.set_handle_redirect(True)
            browser.set_handle_robots(False)
            bs = WebsiteAccount(account['ipaddr'],
                                account['user'],
                                account['pass'],
                                browser)
            # DEBUG
            if opts.debug == True:
                browser.set_debug_redirects(True)
                browser.set_debug_responses(True)
                browser.set_debug_http(True)
            bs.login()
            try:
                # ... do some stuff
                # save cookies here?  
                pass
            finally:
                # you shouldn't use this if you are interested in the login cookies
                bs.logout()
    
    
    if __name__=='__main__':
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.