Esempio n. 1
0
class HttpHolderTest(unittest.TestCase):
    '''
    Test Case of SinKVDB
    '''
    headers = {
        # Chrome User-Agent
        'User-Agent':
        'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',
    }

    def __init__(self, methodName):
        unittest.TestCase.__init__(self, methodName)
        # 准备HttpHolder实例
        self.http = HttpHolder(headers=HttpHolderTest.headers)

    def test0OpenBaidu(self):
        print 'html.len=%d bytes' % (len(
            self.http.open_html('http://www.baidu.com')))

    def test1TimeOut(self):
        try:
            self.http.open_html('http://www.baidu.com', timeout=2)
            print 'not time out'
        except:
            print 'time out'

    def test2Cookies(self):
        # Cookie方面的测试
        self.http.open_html('http://www.baidu.com')
        # get cookies
        cks = self.http.get_cookiesdict()
        print 'get cookies(self.http):'
        for k, v in cks.items():
            print '\t%s=%s' % (k, v)

        self.http2 = HttpHolder(headers=HttpHolderTest.headers)
        self.http2.set_cookiesdict(cks)
        self.http2.open_html('http://www.baidu.com')
        print 'get cookies(self.http2):'
        for k, v in self.http2.get_cookiesdict().items():
            print '\t%s=%s' % (k, v)

    def test3Url(self):
        # 这个请求会被重定向
        self.http.open('http://t.cn/8s954lJ')
        print 'redirect to: %s' % self.http.geturl()

    def test4Doc(self):
        # 文档对象
        doc = self.http.open('http://www.baidu.com')
        print 'response(%s):' % doc.code
        for k, v in doc.headers.dict.items():
            print '\t%s=%s' % (k, v)
Esempio n. 2
0
class HttpHolderTest(unittest.TestCase):
    '''
    Test Case of SinKVDB
    '''
    headers = {
           # Chrome User-Agent
           'User-Agent':'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',
           }
    def __init__(self, methodName):
        unittest.TestCase.__init__(self, methodName)
        # 准备HttpHolder实例
        self.http = HttpHolder(headers=HttpHolderTest.headers)
    
    def test0OpenBaidu(self):
        print 'html.len=%d bytes' % (len(self.http.open_html('http://www.baidu.com')))
    
    def test1TimeOut(self):
        try:
            self.http.open_html('http://www.baidu.com', timeout=2)
            print 'not time out'
        except:
            print 'time out'

    def test2Cookies(self):
        # Cookie方面的测试
        self.http.open_html('http://www.baidu.com')
        # get cookies
        cks = self.http.get_cookiesdict()
        print 'get cookies(self.http):'
        for k, v in cks.items():
            print '\t%s=%s' % (k, v)
        
        self.http2 = HttpHolder(headers=HttpHolderTest.headers)
        self.http2.set_cookiesdict(cks)
        self.http2.open_html('http://www.baidu.com')
        print 'get cookies(self.http2):'
        for k, v in self.http2.get_cookiesdict().items():
            print '\t%s=%s' % (k, v)
    
    def test3Url(self):
        # 这个请求会被重定向
        self.http.open('http://t.cn/8s954lJ')
        print 'redirect to: %s' % self.http.geturl()

    def test4Doc(self):
        # 文档对象
        doc = self.http.open('http://www.baidu.com')
        print 'response(%s):' % doc.code
        for k, v in doc.headers.dict.items():
            print '\t%s=%s' % (k, v)
Esempio n. 3
0
class BaseBaiduUser():
	'''
	基础百度用户
	'''
	def __init__(self, cookies):
		'''
		创建一个BaseBaiduUser实例,需指定可用的Cookie,主要是BDUSS值
		'''
		self.http = HttpHolder(headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36'})
		self.http.set_cookiesdict(cookies)

	def verifyCookie(self):
		'''
		检查当前Cookie是否可用
		'''
		url = 'http://i.baidu.com/center'
		self.http.open_html('http://i.baidu.com/center')
		return self.http.geturl() == url
	
	def sendMessage(self, touser, message='message'):
		'''
		发送消息
		touser, 消息目的用户名
		message, 消息内容
		'''
		form = {
			'msgcontent':message,
			'msgreceiver':touser,
			'refmid':'5255694744',
			'vcode':'',
			'msgvcode':'',
			'bdstoken':'7e1fee8373e8883e95da301f7d2194ec',
			'qing_request_source':''
			}
		head = {
			'Origin':'http://msg.baidu.com',
			'Referer':'http://msg.baidu.com/?t=%d' % (time.time() - 100),
			'X-Requested-With':'XMLHttpRequest'
			}
		html = self.http.open_html(url='http://msg.baidu.com/msg/writing/submit/msg', headers=head, data=form)
		return html.find('errorNo : "0"') >= 0
	
	def createText(self, title='Title', content='Content'):
		form = {
			'title':title,
			'content':'<p>%s</p>' % content,
			'private':'',
			'imgnum':'0',
			'bdstoken':'7e1fee8373e8883e95da301f7d2194ec',
			'qbid':'',
			'refer:http':'//hi.baidu.com/home/?from=index',
			'multimedia[]':'',
			'synflag':'',
			'private1':'',
			'qing_request_source':'new_request'
			}
		head = {
			'Origin':'http://hi.baidu.com',
			'Referer':'http://hi.baidu.com/pub/show/createtext',
			'X-Requested-With':'XMLHttpRequest'
			}
		html = self.http.open_html(url='http://hi.baidu.com/pub/submit/createtext', headers=head, data=form)
		return html.find('"errorNo" : "0"') >= 0