0

I'm trying to read a file in source computer (client) from multiple points by threading and send the chunks to destination(server). I've finished the client program but in server side I have 3 problems: 1-I can't join the chunks to a file simultaneously by multithreading.2-I can't increase size of buffer more than 8KB.3-I have unsuccessful sending in some tries.May I ask you to help to complete my code? This is my client code:

import socket
import Queue
import threading
import filechunkio,os.path
class VRCThread(threading.Thread):
    """Mostafa.Hadi"""
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
    def Upload(self):
        file = self.queue.get()
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect(('172.32.60.45', 9999))
            chunk =filechunkio.FileChunkIO('piano.zip', offset=int(file)*8024, bytes=8024)
            k1=chunk.read()
            k1=k1[0:8024]+"piano.zip."+file
            sock.sendall(k1)
            sock.close()
        except Exception:
           print file+" UnSuccessful upload."
        else:
           print file+" Successful upload."
    def run(self):
        while True:
            self.Upload()
def main():
    queue = Queue.Queue()
    print "Connected to server"
    for i in range(8):
        t = VRCThread(queue)
        t.setDaemon(True)
        t.start()
    siz=os.path.getsize("piano.zip")
    num=siz/8024
    print num
    print siz
    for file in range(num):
        queue.put(str(file))
        print file
    queue.join()
if __name__=="__main__":
        main()

Server side code:

import SocketServer
import threading
import Queue

class MyServerThread(threading.Thread):

    def __init__(self, channel, details):
        self.channel = channel
        self.details = details
        threading.Thread.__init__(self)

    def run(self):
        print 'Received connection:', self.details[0]
        self.k= self.channel.recv(8040)
        self.channel.close()
        namef=self.k[8024:]
        v=open(namef,"wb")
        v.write(self.k)
        v.close

        print 'Closed connection:', self.details[0]

class MyThreadedSocketServerHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        thread1 = MyServerThread(self.request, self.client_address)
        thread1.start()
        thread1.join()
if __name__ == '__main__':
    server = SocketServer.TCPServer(('172.32.60.45', 9999), MyThreadedSocketServerHandler)
    server.serve_forever()

1 Answers1

0

Threading in Python is kind of a pain. Because of the global interpreter lock, only one thread can ever be running at one time.

http://docs.python.org/glossary.html#term-global-interpreter-lock

Check this out: http://docs.python.org/library/multiprocessing.html

As far as the actual question, someone completing your program for you completely deflates the fun of it. Plus, that just smells of homework assignment cheating.

RandomInsano
  • 1,204
  • 2
  • 16
  • 36