Serv-U http下载脚本

Zss 发表于:

测试时需要长时间的下载造成流量和小文件的访问,做设备长时间的代理访问稳定性测试

#coding:utf-8
import requests,re,os,time
from threading import Thread

def get_cookie():
    headers = {'Host':'%s:%s'%(ip,port),
'Connection':'keep-alive',
'Content-Length':'38',
'X-User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
'Origin':'http://%s:%s'%(ip,port),
'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
'Content-Type':'application/x-www-form-urlencoded',
'Accept':'*/*',
'Referer':'http://%s:%s/'%(ip,port),
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.9',
'Cookie':'killmenothing; killmenothing; SULang=zh%2CCN; themename=vista; multitrans=0; multitransbubbletip=false'}
    url = 'http://{}:{}/Web Client/Login.xml?Command=Login&Sync=1560503169683'.format(ip,port)
    data = {'user':'%s'%username,'pword':'%s'%password}
    rsp = requests.post(url,headers=headers,data=data).text
    cookie = re.findall(r'(?:<sessionid>)(.*)(?:</sessionid>)',rsp)[0]
    return cookie

def get_big_file():
    while 1:
        cookie = get_cookie()
        if os.path.isfile('test.exe'):
            os.remove('test.exe')
        headers = {'Host':'[%s]:%s'%(ip,port),
    'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0',
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Accept-Encoding':'gzip, deflate',
    'Referer':'http://{}:{}/Web%20Client/ListDir.htm'.format(ip,port),
    'Connection':'close',
    'Cookie':'killmenothing; SULang=zh%2CCN; themename=vista; multitrans=0; multitransbubbletip=false; Session={}'.format(cookie),
    'Upgrade-Insecure-Requests':'1'}
        url = 'http://{}:{}/?Command=Download&File=%2F{}'.format(ip,port,down_file_name)
        rsp = requests.get(url=url,headers=headers)
        with open('test.exe','wb') as f:
            f.write(rsp.content)
        time.sleep(2)

def get_file():
    while 1:
        cookie = get_cookie()
        headers = {'Host':'[%s]:%s'%(ip,port),
    'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0',
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Accept-Encoding':'gzip, deflate',
    'Referer':'http://{}:{}/Web%20Client/ListDir.htm'.format(ip,port),
    'Connection':'close',
    'Cookie':'killmenothing; SULang=zh%2CCN; themename=vista; multitrans=0; multitransbubbletip=false; Session={}'.format(cookie),
    'Upgrade-Insecure-Requests':'1'}
        url = 'http://{}:{}/?Command=Download&File=%2F{}'.format(ip,port,get_file_name)
        requests.get(url=url,headers=headers)
        time.sleep(0.1)

def main():
    T1 = Thread(target=get_big_file)
    T1.start()
    join_list = []
    for i in range(thread_number):
        t = Thread(target=get_file)
        t.start()
        join_list.append(t)
    for i in join_list:
        i.join()


if __name__ == '__main__':
    if not os.path.isfile('config.ini'):
        with open('config.ini','a') as f:
            f.write('ip-->feca::1:2345:6789:1111\n')
            f.write('port-->8081\n')
            f.write('username-->test\n')
            f.write('password-->test\n')
            f.write('thread_number-->10\n')
            f.write('down_file_name-->test.exe\n')
            f.write('get_file_name-->test1.exe\n')
    with open('config.ini','r') as f:
        data = f.readlines()
    ip = data[0].split('-->')[1]
    port = (data[1].split('-->')[1]).strip()
    username = (data[2].split('-->')[1]).strip()
    password = (data[3].split('-->')[1]).strip()
    thread_number = int((data[4].split('-->')[1]).strip())
    down_file_name = (data[5].split('-->')[1]).strip()
    get_file_name = (data[6].split('-->')[1]).strip()
    if ':' in ip:
        ip = '[' + ip.strip() + ']'
    main()