python server socket file transfer

bingbong3334 at gmail.com bingbong3334 at gmail.com
Tue Jan 9 15:07:29 EST 2018


how much client can i handel whit this code what the amount of client that i can handel
the size of the file is 716 kb


import socket
from threading import Thread
from SocketServer import ThreadingMixIn
## 
TCP_IP = ''
TCP_PORT = 3156
BUFFER_SIZE = 1024

class ClientThread(Thread):

def __init__(self,ip,port,sock):
Thread.__init__(self)
self.ip = ip
self.port = port
self.sock = sock
print " New thread started for "+ip+":"+str(port)

def run(self):
filename='System.exe'
f = open(filename,'rb')
while True:
l = f.read(BUFFER_SIZE)
while (l):
self.sock.send(l)
#print('Sent ',repr(l))
l = f.read(BUFFER_SIZE)
if not l:
f.close()
self.sock.close()
break

tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []

while True:
tcpsock.listen(10000)
print "Waiting for incoming connections..."
(conn, (ip,port)) = tcpsock.accept()
print 'Got connection from ', (ip,port)
newthread = ClientThread(ip,port,conn)
newthread.start()
threads.append(newthread)

for t in threads:
t.join()



and the client side is 


import socket

TCP_IP = ''
TCP_PORT = 3156
BUFFER_SIZE = 1024
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
with open('Sys.exe', 'wb') as f:
print 'file opened'
while True:
#print('receiving data...')
data = s.recv(BUFFER_SIZE)
if not data:
f.close()
print 'file close()'
break
# write data to a file
f.write(data)
except:
print "problem"

print('Successfully get the file')
s.close()
print('connection closed')



More information about the Python-list mailing list