简体   繁体   中英

Python sending files over socket

I want to do a file dispatcher, the server is loaded with a list of files and then sends them one by one with each client request. The idea is to distribute the processing of many files between 5 servers.

How could I call the ClientThread class with each client connection?

The script is only programmed to send the same file to each client request, what I want is to send a different file from a list of files in each client request.

Server.py

import socket
from threading import Thread
from socketserver import ThreadingMixIn

TCP_IP = '10.30.16.28'
TCP_PORT = 1006
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='log.VW.20170214a.log'
        f = open(filename,'rb')
        while True:
            l = f.read(BUFFER_SIZE)
            while (l):
                self.sock.send(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 = []

with open("list.txt") as x: #File containing files list
    lines=x.read().splitlines()
while True:
    tcpsock.listen(5)
    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()

Client.py

import socket

TCP_IP = '10.30.16.28'
TCP_PORT = 1006
BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
with open('received_file', 'wb') as f:
    print('file opened')
    while True:
        data = s.recv(BUFFER_SIZE)
        if not data:
            f.close()
            print('file close()')
            break
    f.write(data)

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

I don't see what this has to do with threading or ports or anything like that. Change this:

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

def run(self):
    f = open(self.fname,'rb')

And when you serve:

with open("list.txt") as x: #File containing files list
    lines=iter(x.read().splitlines())

and finally:

newthread = ClientThread(ip,port,conn,lines.next().strip())

lines.next() will throw a StopIteration exception when done, so you have to handle that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM