Python: Transfer File Multithread di Localhost

From OnnoWiki
Jump to navigation Jump to search

Agar server dapat berinteraksi dengan banyak client, kita perlu menggunakan multi-threading. Berikut adalah script server yang dapat menerima sambungan banyak client.

Server Script

# server2.py
import socket
from threading import Thread
from SocketServer import ThreadingMixIn

TCP_IP = 'localhost'
TCP_PORT = 9001
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='mytext.txt'
        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(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

# client2.py
#!/usr/bin/env python

import socket 

TCP_IP = 'localhost'
TCP_PORT = 9001
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:
        #print('receiving data...')
        data = s.recv(BUFFER_SIZE)
        print('data=%s', (data))
        if not data:
            f.close()
            print 'file close()'
            break
        # write data to a file
        f.write(data) 

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

Output

Di bawah ini adalah output di server console jika kita menjalankan dua client sekaligus:

Waiting for incoming connections...
Got connection from  ('127.0.0.1', 51862)
 New thread started for 127.0.0.1:51862
Waiting for incoming connections...
Got connection from  ('127.0.0.1', 51863)
 New thread started for 127.0.0.1:51863
Waiting for incoming connections...
Got connection from  ('127.0.0.1', 51864)
 New thread started for 127.0.0.1:51864
Waiting for incoming connections...
Got connection from  ('127.0.0.1', 51865)
 New thread started for 127.0.0.1:51865
Waiting for incoming connections...

Referensi

Pranala Menarik