Share python-enabled login and operation scripts, and share python-implemented scripts

Source: Internet
Author: User

Share python-enabled login and operation scripts, and share python-implemented scripts

I have never loved SNS. This time, I wrote a script to log on to kaixin001 and send intra-site messages to all my friends.

Kaixinnet did some processing during login without passing the original password. The result from js analysis is that a random key will be generated during login, then, use the key and the original password for xxtea encryption, and then encrypt the encrypted result with sha1. Then post the key and the encrypted password for Logon verification.

The following is a simple script:

# Coding: UTF-8 "kaixinnet operation script Author: piglei2007@gmail.comVersion: 1.0 "" import reimport into urllib2import randomimport hashlibimport binasciiimport cookielibimport simplejsonfrom xxtea import sources = "http://www.kaixin001.com/login/login_api.php" sources = "http://www.kaixin001.com/" sources = "http://www.kaixin001.com/interface/suggestfriend.php" MESSAGE_SEND_URL = "http: // Www.kaixin001.com/msg/post.php "LOGIN_KEY_RE = re. compile (r" new \ sEnLogin \('(.*?) '") Class LoginError (Exception):" Logon Failure throws an Exception "class Kaixin001User (object):" operating on kaixin001. The existing method is as follows: get_login_key-get the Encrypted key get_rpassword allocated when the user accesses the logon page-get the password login encrypted by xxtea and sha1-log on to get_friends_list-Get all friends, return the dictionary format send_messages_to_all-send a message to all friends "def _ init _ (self, username, password): self. username = username self. password = password self. cj = cookielib. cookieJar () opener = urllib2.build _ ope Ner (urllib2.HTTPCookieProcessor (self. cj) opener. addheaders = [("User-agent", "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv: 1.9.1) Gecko/20090704 Firefox/3.5 "), ("Accept", "*/*"), ("Host", "www.kaixin001.com")] urllib2.install _ opener (opener) def get_login_key (self ): "obtain the Encrypted key for Logon" "_ temp = urllib2.urlopen (LOGIN_KEY_URL ). read () key = LOGIN_KEY_RE.search (_ temp ). group (1) return key def login (s Elf): "login" "login_key = self. get_login_key () rpassword = self. get_rpassword (self. password, login_key) login_params = {'email ': self. username, 'encypt': login_key, 'rpasswd': rpassword, 'url': '/home/', 'ver ': '1'} req = urllib2.Request (LOGIN_URL, urllib. urlencode (login_params), {"Referer": "http://www.kaixin001.com/"}) result = urllib2.urlopen (req ). read () # logon failure if "errno" in result: rais E LoginError ("Logon Failed, please check username or password") print "User % s login successful! "% Self. username return 'OK' def get_friends_list (self): "Get all friends list" "get_friends_params = {'T': str (random. random (), 'type': 'all',} result = urllib2.urlopen (FRIEND_LIST_URL, urllib. urlencode (get_friends_params )). read () friends = simplejson. loads (result) print "you have a total of % s friends" % (len (friends)-1) return friends def send_messages_to_all (self, message = ''): "send messages to all friends" friends = self. get_friends_list () send_params = {'attachment _ cancel': '', 'attachment _ forwarding ':'', 'attachment _ random': '', 'code ':'', 'content': message, 'forward _ thread': '', 'rcode':'', 'service': '0', 'texttype': 'html ', 'ukids ':",". join ([str (f ['uid']) for f in friends])} result = urllib2.urlopen (MESSAGE_SEND_URL, urllib. urlencode (send_params) print result. geturl () print "message sent successfully" return 'OK' def get_rpassword (self, password, key): "encrypted password" xxtea_pw = binascii. b2a_hex (encrypt (password, key) r_password = hashlib. sha1 (xxtea_pw ). hexdigest () return r_password if _ name _ = '_ main _': kxu = Kaixin001User (username = 'your _ username ', password = 'your _ password') kxu. login () kxu. send_messages_to_all ("This message is send by Python. ")

This is the python implementation (xxtea. py) of the xxtea Algorithm Used in the script ):

import struct _DELTA = 0x9E3779B9  def _long2str(v, w):   n = (len(v) - 1) << 2   if w:     m = v[-1]     if (m < n - 3) or (m > n): return ''     n = m   s = struct.pack('<%iL' % len(v), *v)   return s[0:n] if w else s  def _str2long(s, w):   n = len(s)   m = (4 - (n & 3) & 3) + n   s = s.ljust(m, "\0")   v = list(struct.unpack('<%iL' % (m >> 2), s))   if w: v.append(n)   return v  def encrypt(str, key):   if str == '': return str   v = _str2long(str, True)   k = _str2long(key.ljust(16, "\0"), False)   n = len(v) - 1   z = v[n]   y = v[0]   sum = 0   q = 6 + 52 // (n + 1)   while q > 0:     sum = (sum + _DELTA) & 0xffffffff     e = sum >> 2 & 3     for p in xrange(n):       y = v[p + 1]       v[p] = (v[p] + ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z))) & 0xffffffff       z = v[p]     y = v[0]     v[n] = (v[n] + ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[n & 3 ^ e] ^ z))) & 0xffffffff     z = v[n]     q -= 1   return _long2str(v, False)  def decrypt(str, key):   if str == '': return str   v = _str2long(str, False)   k = _str2long(key.ljust(16, "\0"), False)   n = len(v) - 1   z = v[n]   y = v[0]   q = 6 + 52 // (n + 1)   sum = (q * _DELTA) & 0xffffffff   while (sum != 0):     e = sum >> 2 & 3     for p in xrange(n, 0, -1):       z = v[p - 1]       v[p] = (v[p] - ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z))) & 0xffffffff       y = v[p]     z = v[n]     v[0] = (v[0] - ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[0 & 3 ^ e] ^ z))) & 0xffffffff     y = v[0]     sum = (sum - _DELTA) & 0xffffffff   return _long2str(v, True)  if __name__ == "__main__":   print decrypt(encrypt('Hello XXTEA!', '16bytelongstring'), '16bytelongstring')


The simplest python script, file operations, and help

The idea should be to open the t2 file, retrieve the regular expression, open the t1 file, use the regular expression to match, and put all the matching results into the result, finally, the result field is output.

Python script to automatically log on to the Single Sign-On System

Single Sign On (SSO) is one of the most popular solutions for business integration. SSO is defined in multiple application systems. Users only need to log on once to access all mutually trusted application systems. It includes a mechanism for ing the main logon to other applications for the login of the same user. When a user accesses Application System 1 for the first time, the user is directed to the authentication system for Logon because the user has not yet logged on. Based on the login information provided by the user, the authentication system performs identity verification, if q is verified, the user should be returned with an authentication credential-ticket; when the user accesses another application, the ticket will be taken with the OS as the authentication credential, after receiving the request, the application system sends the ticket to the authentication system for verification. If the validity of the ticket is verified, you can access application system 2 and Application System 3 without logging on again.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.