python - Make Post Requests with Files Simultaneously -


i write simple server , runs well. want write codes make many post requests server simultaneously simulate pressure test. use python.

suppose url of server http://myserver.com. file1.jpg , file2.jpg files needed uploaded server. here testing code. use threading , urllib2.

async_posts.py

from queue import queue threading import thread poster.encode import multipart_encode poster.streaminghttp import register_openers import urllib2, sys  num_thread = 4 queue = queue(2*num_thread) def make_post(url):     register_openers()     data = {"file1": open("path/to/file1.jpg"), "file2": open("path/to/file2.jpg")}     datagen, headers = multipart_encode(data)     request = urllib2.request(url, datagen, headers)     start = time.time()     res = urllib2.urlopen(request)     end = time.time()     return res.code, end - start # return status code , duration of request.  def deamon():     while true:         url = queue.get()         status, duration = make_post(url)         print status, duration         queue.task_done()  _ in range(num_thread):     thd = thread(target = daemon)     thd.daemon = true     thd.start()  try:     urls = ["http://myserver.com"] * num_thread     url in urls:         queue.put(url)     queue.join() except keyboardinterrupt:     sys.exit(1) 

when num_thread small (ex: 4), code runs smoothly. switch num_thread larger number, 10, threading things break down , keep throwing httplib.badstatusline error.

i don't know why code goes wrong or maybe there better way this?

a reference, server written in python using flask , gunicorn.

thanks in advance.


Comments