python多线程get与post请求模板代码

来源:互联网 发布:淘宝网首页秋水伊人 编辑:程序博客网 时间:2024/05/20 00:35

post请求模板代码:

#coding=utf-8'''random.randint(a, b):用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= brandom.choice(sequence):从序列中获取一个随机元素参数sequence表示一个有序类型(列表,元组,字符串)'''import httplib,urllibfrom time import ctime  import threading#from random import randint,choice #创建请求函数def postRequest():    postJson={         }    #定义需要进行发送的数据    params = urllib.urlencode(postJson);    #定义一些文件头    headers = {           }        #接口    requrl =""        #请求服务,例如:www.baidu.com    hostServer=""    #连接服务器           conn = httplib.HTTPConnection(hostServer)    #发送请求           conn.request(method="POST",url=requrl,body=params,headers=headers)    #获取请求响应           response=conn.getresponse()    #打印请求状态    if response.status in range(200,300):        pass                  #创建数组存放线程    threads=[] #创建100个线程for i in range(100):    #针对函数创建线程      t=threading.Thread(target=postRequest,args=())    #把创建的线程加入线程组         threads.append(t)    if __name__ == '__main__':    #启动线程      for i in threads:          i.start()      #keep thread      for i in threads:          i.join()


get请求模板代码:

#coding=utf-8'''random.randint(a, b):用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= brandom.choice(sequence):从序列中获取一个随机元素参数sequence表示一个有序类型(列表,元组,字符串)'''import httplib,urllibfrom time import ctime  import threading#from random import randint,choice #创建请求函数def getRequest():        #定义一些文件头    headers = {           }        #请求服务,例如:www.baidu.com    hostServer=""    #接口    requrl =""    #连接服务器           conn = httplib.HTTPConnection(hostServer)    #发送请求           conn.request(method="GET",url=requrl,headers=headers)    #获取请求响应           response=conn.getresponse()    #打印请求状态    if response.status in range(200,300):        pass                  #创建数组存放线程    threads=[] #创建100个线程for i in range(100):    #针对函数创建线程      t=threading.Thread(target=getRequest,args=())    #把创建的线程加入线程组         threads.append(t)     if __name__ == '__main__':    #启动线程      for i in threads:          i.start()      #keep thread      for i in threads:          i.join()        


以上方法,只适用于基本的,其他的需要进行自己修改!

原创粉丝点击