Created
August 1, 2012 21:28
-
-
Save chm0815/3230907 to your computer and use it in GitHub Desktop.
Facebook Login Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, urllib, urllib2, cookielib | |
class FacebookLogin(object): | |
def __init__(self,user,passw): | |
self.user=user | |
self.passw=passw | |
self.browser = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) | |
self.browser.addheaders=[('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0')] | |
urllib2.install_opener(self.browser) | |
def login(self): | |
params=urllib.urlencode({'email': self.user, 'pass': self.passw}) | |
#now login | |
print 'Logging in to account ' + self.user | |
res=self.browser.open("https://www.facebook.com/login.php?m=m&refsrc=http://m.facebook.com/home.php&refid=8", params) | |
if "login" in res.url: | |
print "Login failed!" | |
exit(-1) | |
else: | |
print "Login ok!" | |
res.close() | |
return self.browser | |
def logout(self): | |
print 'Logging out ' + self.user | |
res=self.browser.open("http://m.facebook.com/logout.php?h=d439564b69cfc8f1cbca42beb7726b77&t=1314710986&refid=5&ref=mfl") | |
def main(): | |
fblogin=FacebookLogin("[email protected]","password343434") | |
fblogin.login() | |
fblogin.logout() | |
if __name__ == '__main__': | |
main() |
This does not work
import sys
import urllib.request
import urllib.parse
import urllib.error
import http.cookiejar
class FacebookLogin(object):
def __init__(self, user, passw):
self.user = user
self.passw = passw
# Create a cookie jar to handle cookies
self.cookie_jar = http.cookiejar.CookieJar()
# Build an opener with the cookie jar
self.browser = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cookie_jar))
# Add a user-agent header to the opener
self.browser.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0')]
# Install the opener globally so that urllib can use it
urllib.request.install_opener(self.browser)
def login(self):
# URL-encode the login parameters
params = urllib.parse.urlencode({'email': self.user, 'pass': self.passw}).encode('utf-8')
# Print the login attempt
print('Logging in to account ' + self.user)
# Attempt to open the login URL with the parameters
res = self.browser.open("https://www.facebook.com/login.php?m=m&refsrc=http://m.facebook.com/home.php&refid=8", params)
# Check if "login" is in the URL to determine if login was successful
if "login" in res.geturl():
# Print login failure message
print("Login failed!")
sys.exit(-1)
else:
# Print login success message
print('Login ok')
# Close the response
res.close()
return self.browser
def logout(self):
# Print the logout attempt
print('Logging out ' + self.user)
# Attempt to open the logout URL
self.browser.open("http://m.facebook.com/logout.php?h=d439564b69cfc8f1cbca42beb7726b77&t=1314710986&refid=5&ref=mfl")
def main():
# Create a FacebookLogin instance with the given user credentials
fblogin = FacebookLogin("[email protected]", "password343434")
# Attempt to login
fblogin.login()
# Attempt to logout
fblogin.logout()
# Ensure the script runs only if it's executed directly
if __name__ == '__main__':
main()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
250 script