Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*-coding:u8-*-
- # @author <tz4678@gmail.com>
- # / /¯¯
- # /__/__
- # / /
- # __/ /
- import urllib
- import urllib2
- import cookielib
- class WebBot(object):
- def __init__(self):
- self.cj = cookielib.CookieJar()
- self.opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(self.cj) )
- self.default_headers = [('User-Agent', 'Mozilla/5.0')]
- def request(self, url, data=None, headers={}):
- self.opener.addheaders = list(self.default_headers)
- for header in headers.items():
- self.opener.addheaders.append(header)
- return self.opener.open(url, data)
- def get(self, url, data=None, headers={}):
- r = self.request(url, data, headers)
- content = r.read()
- ct = r.info().getheader('content-type')
- try:
- m = re.search(r'(?i)\bcharset\s*=\s*([^;]*)', ct)
- content = content.decode( m.group(1) )
- except:
- pass
- return content
- def post(self, url, parameters, headers={}):
- data = urllib.urlencode(parameters)
- headers = dict(headers)
- headers['content-type'] = 'application/x-www-form-urlencoded'
- return self.get(url, data, headers)
- def upload(self, url, fields={}, files={}):
- ...
- def download(self, url, dst):
- def login(self, username, password):
- r = self.get('http://m.mail.ru/cgi-bin/auth?Login=' + urllib.quote(username)+ '&Password=' + urllib.quote(password))
- if r.geturl()[0:33] == 'https://e.mail.ru/messages/inbox/':
- return r.read().decode('u8')
- raise Exception('Authorization failed: invalid username or password.')
- def clear_cookies(self):
- self.cj.clear()
- if __name__ == '__main__':
- bot = MailBot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement