def test_session(self): headers = testlib.randdict() cookies = testlib.randdict() s = urlfetch.Session(headers=headers, cookies=cookies) self.assertEqual(s.snapshot(), { 'headers': headers, 'cookies': cookies }) randstr = testlib.randstr header = (randstr(), randstr()) headers[header[0]] = header[1] s.putheader(*header) self.assertEqual(s.snapshot(), { 'headers': headers, 'cookies': cookies }) cookie = (randstr(), randstr()) cookies[cookie[0]] = cookie[1] s.putcookie(*cookie) self.assertEqual(s.snapshot(), { 'headers': headers, 'cookies': cookies }) header = headers.popitem() s.popheader(header[0]) self.assertEqual(s.snapshot(), { 'headers': headers, 'cookies': cookies }) cookie = cookies.popitem() s.popcookie(cookie[0]) self.assertEqual(s.snapshot(), { 'headers': headers, 'cookies': cookies }) s = urlfetch.Session() cookie = (randstr(), randstr()) r = s.get(testlib.url('setcookie/%s/%s' % cookie)) self.assertEqual(s.cookies[cookie[0]], cookie[1]) self.assertTrue(('%s=%s' % cookie) in s.cookiestring) cookie2 = (randstr(), randstr()) r = s.get(testlib.url('setcookie/%s/%s' % cookie2)) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookie)) self.assertEqual(s.cookies[cookie[0]], cookie[1]) self.assertTrue(('%s=%s' % cookie) in s.cookiestring) self.assertEqual(s.cookies[cookie2[0]], cookie2[1]) self.assertTrue(('%s=%s' % cookie2) in s.cookiestring)
def test_cookiestring_setter(self): headers = testlib.randdict() cookies = testlib.randdict() s1 = urlfetch.Session(headers=headers, cookies=cookies) headers = testlib.randdict() cookies = testlib.randdict() s2 = urlfetch.Session(headers=headers, cookies=cookies) s2.cookiestring = s1.cookiestring self.assertEqual(s1.cookies, s2.cookies) s1.cookiestring = '' self.assertEqual(s1.cookies, {})
def test_session_fragment_query_string(self): qs = testlib.randdict(5) query_string = urlfetch.urlencode(qs) cookiepair = (testlib.randstr(), testlib.randstr()) headerpair = (testlib.randstr(), testlib.randstr()) s = urlfetch.Session(headers=dict((headerpair, )), cookies=dict((cookiepair, ))) r = s.get(testlib.url()) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'GET') self.assertEqual(r.reqheaders[headerpair[0]], headerpair[1]) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookiepair)) r = s.get(testlib.url('?' + query_string + '#urlfetch')) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'GET') self.assertEqual(r.reqheaders[headerpair[0]], headerpair[1]) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookiepair)) self.assertEqual(o['query_string'], query_string) self.assertEqual(o['get'], qs)
def test_session_get(self): cookiepair = (testlib.randstr(), testlib.randstr()) headerpair = (testlib.randstr(), testlib.randstr()) s = urlfetch.Session(headers=dict((headerpair, )), cookies=dict((cookiepair, ))) r = s.get(testlib.url()) o = json.loads(r.text) self.assertEqual(r.status, 200) self.assertTrue(isinstance(r.json, dict)) self.assertTrue(isinstance(r.text, urlfetch.unicode)) self.assertEqual(o['method'], 'GET') self.assertEqual(r.reqheaders[headerpair[0]], headerpair[1]) self.assertEqual(r.reqheaders['Cookie'], '='.join(cookiepair))
def pub2fanfou(username, password, status): #获取表单token s = urlfetch.Session(headers={'Referer': 'http://m.fanfou.com/'}) response = s.fetch( "http://m.fanfou.com/" ) token = re.search('''name="token".*?value="(.*?)"''', response.body).group(1) #登录 response = s.fetch( "http://m.fanfou.com/", data = { 'loginname': username, 'loginpass': password, 'action': 'login', 'token': token, 'auto_login': '******', }, ) #cookies print s.cookiestring #获取表单token response = s.fetch( "http://m.fanfou.com/home", ) token = re.search('''name="token".*?value="(.*?)"''', response.body).group(1) #发布状态 response = s.fetch( "http://m.fanfou.com/", data = { 'content': status, 'token': token, 'action': 'msg.post', }, )
#!/usr/bin/env python import urlfetch import logging import json import os from datetime import date, datetime, timedelta # See inspiration from https://github.com/Flet/dailyconnect # Uses ENV vars: # * DC_EMAIL: Your login to DailyConnect # * DC_PASS: Your password to DailyConnect # * DC_STARTDATE: The first date to gather photos # * DC_ENDDATE: The last date to gather photos s = urlfetch.Session() r = s.post('https://www.dailyconnect.com/Cmd?cmd=UserAuth', data={ "email": os.environ['DC_EMAIL'], "pass": os.environ['DC_PASS'] }) r = s.post('https://www.dailyconnect.com/CmdW', data={"cmd": "UserInfoW"}) j = json.loads(r.body) myKids = j['myKids'] start_date = datetime.strptime(os.environ['DC_STARTDATE'], "%y%m%d") end_date = datetime.strptime(os.environ['DC_ENDDATE'], "%y%m%d") delta = end_date - start_date for d in (start_date + timedelta(n) for n in range(delta.days + 1)): pdt = d.strftime("%y%m%d") print(pdt) for kid in myKids:
def test_timeout(self): self.assertRaises( urlfetch.Timeout, lambda: urlfetch.Session().get( testlib.url('sleep/1'), timeout=0.5))
def __init__(self, username, password): self.username = username self.password = password self.session = urlfetch.Session( headers={'Referer': 'http://v2ex.com/'})