Can python be used to log onto password protected sites for checking gmail, forums, etc...?
I know you can open webpages quite simply using the webbrowser module like:
import webbrowser
webbrowser.open('http://www.google.com')
but is it possible to pass log-in credentials to these websites? I would like to create a script that automates some stuff that I do on a regular basis, for example, open gmail in firefox to check my mail.
In addition, if it is possible to do so, can you somehow provide your password in this script in a secure way? So rather than plainly saying in the code:
pw = "defaultPass123"
webbrowser.sendPass(pw) # made up function, I don't think this actually works :)
you could somehow encrypt this开发者_如何学C data?
Depending on how the server handles authentication, you might be able to create a GET request that would do the trick:
from urllib import urlencode
credentials = {
'username': 'bob',
'password': 'secret'}
url = "http://example.com/login?" + urlencode(credentials)
Whether this will work depends on the service you are trying to access. You may be able to authenticate using a POST request, but if the goal is to open the site in an external browser, there aren't any consistent ways of passing POST request details to an external browser.
You could encrypt the password, yes. But since the script would need to decrypt the password in order to send it to the server, anyone with access to the script can probably still read your password. (Though it might keep casual non-programmer snoopers from reading your password.)
yes it is possible via the use of OpenID.
http://openid.net/
OpenID basically is a login mechanism that allows users/programs to login WITHOUT exposing their login name and password. As far as I know, if you have a Google Account, you already can use OpenID, many others implement OpenID too, like Yahoo Facebook MySpace Flickr etc
Is your question specifically about accessing Google's sites, or about sites in general? If it is the former then Google provide APIs for most of their services and usually include Python as a supported language. For example, to check your Gmail with Python follow the instructions on http://code.google.com/p/google-mail-xoauth-tools/wiki/XoauthDotPyRunThrough
Even outside the Google world the first question you should be asking is 'does this site expose an api?'. If it does then that will be much easier to use than either simulating a web browser or driving a browser remotely.
If you decide you do just want to drive a browser remotely then look at Selenium as that will give you an easy way to drive almost any web browser from a separate application.
精彩评论