Advertisement
stuppid_bot

Класс для работы с почтой mail.ru

Aug 27th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. # -*-coding:u8-*-
  2. # @author <tz4678@gmail.com>
  3. #   /  /¯¯
  4. #  /__/__
  5. #    /  /
  6. # __/  /
  7. import urllib
  8. import urllib2
  9. import cookielib
  10.  
  11. class WebBot(object):
  12.     def __init__(self):
  13.         self.cj = cookielib.CookieJar()
  14.         self.opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(self.cj) )
  15.         self.default_headers = [('User-Agent', 'Mozilla/5.0')]
  16.  
  17.     def request(self, url, data=None, headers={}):
  18.         self.opener.addheaders = list(self.default_headers)
  19.         for header in headers.items():
  20.             self.opener.addheaders.append(header)
  21.         return self.opener.open(url, data)
  22.  
  23.     def get(self, url, data=None, headers={}):
  24.         r = self.request(url, data, headers)
  25. content = r.read()
  26.         ct = r.info().getheader('content-type')
  27. try:
  28. m = re.search(r'(?i)\bcharset\s*=\s*([^;]*)', ct)
  29. content = content.decode( m.group(1) )
  30. except:
  31. pass
  32. return content
  33.  
  34. def post(self, url, parameters, headers={}):
  35. data = urllib.urlencode(parameters)
  36. headers = dict(headers)
  37. headers['content-type'] = 'application/x-www-form-urlencoded'
  38. return self.get(url, data, headers)
  39.  
  40. def upload(self, url, fields={}, files={}):
  41. ...
  42.  
  43. def download(self, url, dst):
  44.            
  45.     def login(self, username, password):
  46.         r = self.get('http://m.mail.ru/cgi-bin/auth?Login=' + urllib.quote(username)+ '&Password=' + urllib.quote(password))
  47.         if r.geturl()[0:33] == 'https://e.mail.ru/messages/inbox/':
  48.             return r.read().decode('u8')
  49.         raise Exception('Authorization failed: invalid username or password.')
  50.  
  51.     def clear_cookies(self):
  52.         self.cj.clear()
  53.        
  54.  
  55. if __name__ == '__main__':
  56.     bot = MailBot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement