構造數據包進行批量用戶註冊

 長知識:json

現大部分服務端、客戶端都默認是將數據進行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:\fuck\1.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)
相關文章
相關標籤/搜索