python 携带cookie访问网站(python接口测试post)

  最近在使用自己研究性能测试工具的时候想到,使用python向服务器不断发送数据以作为并发测试。大概情况如下:

#coding=utf-8

import urllib2
import urllib
import cookielib
import os
import threading
import time
class HB:def add_cookie_login(self,username):
self.user = username
cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
headers ={
"User-agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1"}
data={
"loginname":self.user,
"password":'',
'remember':1}
postdata = urllib.urlencode(data) url_1 = 'http://test.reg.haibian.com/login/ajax_login'
request = urllib2.Request(url_1,postdata,headers)
url =urlOpener.open(request)
print url.info()
page = url.read()
url = urlOpener.open('http://test.www.haibian.com/home/index')
#request = urllib2.Request(url)
#url = urlOpener.open(request)
page = url.read()
url = urlOpener.open('http://test.www.haibian.com/course/study?chapter_id=8540&course_id=1095&class_id=1433152942&date_id=48946')
page = url.read()
print page def users(self):
f = open('student_email.txt')
data = f.readlines()
for user in data:
self.add_cookie_login(user)
break
#print user,
pass
if __name__ == '__main__':
l = HB()
l.users()

  就是通过python的几个包,然后将数据发送到服务器,然后再已服务器返回的登录cookie访问网站其他的页面。

上一篇:shell 编程每日100行


下一篇:DevExpress 标题栏添加搜索功能