http.client.RemoteDisconnected: Remote end closed connection without response

Deadly 提交于 2019-12-11 04:36:58

问题


I am using the http.client module to download files from an apache server. Basically each thread is downloading files from the server. But when there are a large number of threads running I seem to be facing a problem where some threads get terminated. This is the main part of my code :

   def down(i,t_end,con,s_ip):
    num = 0
    while time.time()<t_end:
        con[i].request("GET","/"+file_name)
        r1 = con[i].getresponse()
        data1 = r1.read()
        handle = open('/home/client1/test/try'+str(num)+str(i)+'.txt','wb')
        handle.write(data1)
        handle.close()
        num+=1


    print("number of files downloaded for user"+str(i)+"="+str(num))    


if __name__=='__main__': 
    t=input('enter time in seconds\t')
    ip=input('enter server ip address (default value:192.168.37.3)\t')
    n=input('enter no of users (default value:1)\t')
    file_name=input('Enter the file name (default value:testing2.txt)\t')
    pr=input('enter protocol (default value:http)\t')
    if t=='':
        t=2
    if ip=='':
        ip='192.168.37.3'
    if file_name=='':
        file_name='testing2.txt'
    if pr=='':
        pr='http'
    if n=='':
        n=1
    s_ip = dict()
    con = dict()

    for i in range(5,5+int(n)):
        s_ip[i-5]='192.168.37.'+str(i)

        os.system('sudo ip address add '+s_ip[i-5]+'/24 dev ens33')
        con[i-5] = http.client.HTTPConnection(ip,source_address=(s_ip[i-5],80))
    t_end = time.time()+float(t)
    for i in range(int(n)):
        try:
            t1 = threading.Thread(target=down,args=(i,t_end,con,s_ip))
            procs.append(t1)
            t1.start()

        except Exception as e:
            print(str(e))

If I increase the number of threads I get http.client.RemoteDisconnected: Remote end closed connection without response for certain threads. What could be causing this problem?

These are errors for multiple threads

Exception in thread Thread-41:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "single2.py", line 16, in down
    r1 = con[i].getresponse()
  File "/usr/lib/python3.5/http/client.py", line 1197, in getresponse
    response.begin()
  File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.5/http/client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

来源:https://stackoverflow.com/questions/49150849/http-client-remotedisconnected-remote-end-closed-connection-without-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!