Share a common Python mock login class _python

Source: Internet
Author: User

The code is very simple, and the annotation is very detailed, there is no more nonsense

tools.py

#-*-Coding:utf8-*-' # ============================================================================= # filename:t ools.py # Desc: Analog Browser # author:cosven # email:yinshaowen241@gmail.com # HomePage:www.cosven.com # Versi on:0.0.1 # lastchange:2015-03-27 00:59:24 # History: # ============================================================= ================ ' Import urllib import urllib2 Import Cookielib class MyWeb (): "" Simulate a Browser "" "Def __  Init__ (self): Self.header = {' Host ': ' music.163.com ', ' content-type ': ' application/x-www-form-urlencoded; Charset=utf-8 ", ' Referer ': ' http://music.163.com/song?id=26599525 '," user-agent ":" opera/8.0 "(Macintosh; PPC Mac OS X; U EN) "} Self.cookie = Cookielib. Lwpcookiejar () Self.cookie_support = Urllib2. Httpcookieprocessor (self.cookie) Self.opener = Urllib2.build_opener (Self.cookie_support, URLLIB2 . HttpHandler) Urllib2.install_opener (self. opener) def post (self, PostURL, dictdata): "" "simulate Post request:p Aram string Posturl:url address:p Aram Dict di Ctdata: Sent Data "" "PostData = Urllib.urlencode (dictdata) request = Urllib2. Request (PostURL, PostData, self.header) try:content = Urllib2.urlopen (Request) return content except Exception, E:print ("Post:" + str (e)) return the None def get (self, URL): "" "Simulate GET request:p Aram ur L:url Address: Return content: Often read the returned data using the Read method: Rtype:instance or None "" "Request = Urllib2. Request (URL, None, self.header) try:content = Urllib2.urlopen (Request) return content except Exceptio N, E:print ("Open:" + str (e)) return the None if __name__ = "__main__": Import hashlib web = MyWeb () u RL = ' http://music.163.com/api/login/' data = {' username ': ' username ', # email ' password ': hashlib.md5 (' Passwor d '). Hexdigest (), # password ' rememberlogin ': ' true '} res = web.Post (URL, data) print Res.read () # url_add = ' http://music.163.com/api/playlist/manipulate/tracks ' # data_add = { # ' tracks ': ' 26599525 ', # Music ID # ' PID ': ' 16199365 ', # Playlist ID # ' trackids ': ' [' 26599525 '] ', # Music ID STR # ' OP ': ' Add ' # opation #} # res_add = Web.post (Url_add, Data_add) # print res_add.read () # You can try to look at yourself
 NetEase cloud music corresponding list song

The above is the article to share the code, I hope you can enjoy, but also hope to learn Python help.

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.