5

I had learned a lot of things from MOOCs so I wanted to return something back to them for this purpose I was thinking of designing a small app in kivy which thus requires python implementation, Actually the thing I wanted to achieve was to log in to my Coursera account via program and collect the information about the courses I am currently pursuing, for this first I have to log in to the coursera( https://accounts.coursera.org/signin?post_redirect=https%3A%2F%2Fwww.coursera.org%2F ), Upon searching the Web I came across this piece of code :

import urllib2, cookielib, urllib

username = "abcdef@abcdef.com"      
password = "uvwxyz"

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'password' : password})
info = opener.open("https://accounts.coursera.org/signin",login_data)
for line in info:
    print line

and some similar codes as well, but none worked for me, every approach lead to me this type of error:

Traceback (most recent call last):
  File "C:\Python27\Practice\web programming\coursera login.py", line 9, in <module>
    info = opener.open("https://accounts.coursera.org/signin",login_data)
  File "C:\Python27\lib\urllib2.py", line 410, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found

Is the error due to https protocol or there is something that I am missing?

I don't want to use any 3rd party libraries.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ZdaR
  • 22,343
  • 7
  • 66
  • 87

2 Answers2

2

I'm using requests for this purpose and I think it is a great python library. Here is some example code how it could work:

import requests
from requests.auth import HTTPBasicAuth

credentials = HTTPBasicAuth('username', 'password')
response = requests.get("https://accounts.coursera.org/signin", auth=credentials)
print response.status_code
# if everything was fine then it prints
>>> 200

Here is the link to requests:

http://docs.python-requests.org/en/latest/

cezar
  • 11,616
  • 6
  • 48
  • 84
  • Does the return code 200 means that I have successfully logged in to the website or it simply means that the above mention URL was valid ?again when I was trying to open another protected page , it is not opening , do I need to use sessions ? kindly help it's since I week I am struck to this – ZdaR Oct 04 '14 at 07:55
  • Status code 200 should mean that you have successfully logged in. If the credentials provided are not valid, than status code 403 (FORBIDDEN) should be returned. If that's exactly the case with Coursera I can't confirm or test. – cezar Oct 06 '14 at 14:27
1

I think you need to use HTTPBasicAuthHandler module of urllib2. Check section 'Basic Authentication'. https://docs.python.org/2/howto/urllib2.html

And I strongly recommend you requests module. It will make your code better. http://docs.python-requests.org/en/latest/

Stephen Lin
  • 4,852
  • 1
  • 13
  • 26