我正在使用http.client模块从apache服务器下载文件。 基本上每个线程都从服务器下载文件。但是当有大量线程在运行时,我似乎面临着一些线程被终止的问题。 这是我的代码的主要部分:
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))
如果我增加了线程数,我得到http.client.RemoteDisconnected:远程结束关闭连接而没有响应某些线程。 什么可能导致这个问题?
这些是多线程的错误
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