构造数据包进行批量用户注册

 长知识:

现大部分服务端、客户端都默认是将数据进行UTF-8编码,但是当客户端向服务端发送的数据包中含有中文时,一定在headers中说明charset,否则服务器端不知道要用什么编码方式解码。而英文、数字等一般服务器端默认用UTF-8解码。

#coding:UTF-8
import urllib2
import urllib
import cookielib
import json
import sys
import shutil
import xlrd
import chardet
import os
import time
import random

stdout = sys.stdout
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdout = stdout


cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) 

data = xlrd.open_workbook(r'c:fuck1.xls')
table = data.sheets()[0]

num = 100000000
for i in range(0,11):
    name = table.cell(i,0).value[-1]
    name = name + name
    mail = table.cell(i,2).value
    
    for j in range(0,11):
        tnum = unicode(num)
        sname = name
        name2 = table.cell(j,1).value;
        sname = sname + name2
        if(len(sname)<=27):
            sname = unicode("")+sname
        
        
        #需要POST的数据#  
        postdata=urllib.urlencode({    
            'stuDep':'计算机学院',    
            'stuId':tnum,
            'stuMail':mail,
            'stuName':sname,
            'stuPhone':'12345678901'
        })
        headerss = {'Content-Type':
                   'application/x-www-form-urlencoded; charset=UTF-8'
                   };
        
        #自定义一个请求#  
        req = urllib2.Request(    
            url = 'http://172.31.150.21/ACMRegister/test.json',    
            data = postdata , 
            headers = headerss
        ) 
        
        #访问该链接  
        result = opener.open(req)  
          
        print sname + ' finished.   '+result.read()
        
        num += 1
            
        dd = random.random()
        yy = random.randint(0,1)
        time.sleep(dd+yy)
原文地址:https://www.cnblogs.com/fish7/p/4166249.html