Hi, how would I login to an HTTPS site? I used the following code to connect to the site, but how do I login?

import httplib

HOSTNAME = 'login.yahoo.com'
conn = httplib.HTTPSConnection(HOSTNAME)
conn.putrequest('GET', '')
conn.endheaders()
response = conn.getresponse()
print response.read()

I tried a different approach using the mechanize library. I can log in but after I switch to a different page it kicks me out. It doesn't seem to be a cookie problem because the session cookie is still stored even after I'm kicked out. Here's the code:

import cookielib
import mechanize

br = mechanize.Browser()

br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 BTRS28621 Firefox/3.6.16')]

cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

response = br.open("https://webconnect.bloomfield.org/Zangle/StudentConnect/")

br.select_form(nr=0)

br.form['stuident'] = 'id'
br.form['stupassword'] = 'pass'
br.submit()

cj.save("cookie.txt", True, False)

print br.response().read()

br.open("https://webconnect.bloomfield.org/Zangle/StudentConnect/stuassignments.aspx")
print br.response().read()
cj.save("cookie1.txt", True, False)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.