Python multithreaded File transfer paradigm (c/s) __python

Source: Internet
Author: User
Tags server port

Client code:


Import socket

Import OS

Import Sys

Import Math

Import time

Import threading

def getfilesize (file): (0, OS. Seek_end)

Filelength = File.tell () (0, 0)

Return filelength

def getfilename (Filefullpath):

index = filefullpath.rindex (' \ \ ')

If index = = 1:

Return Filefullpath


Return filefullpath[index+1:]

Def transferfile ():

Filefullpath = r "%s"% raw_input ("File path:"). Strip ("\")

If Os.path.exists (Filefullpath):

Timestart = Time.clock ()

File = open (Filefullpath, ' RB ')

FileSize = GetFileSize (file)

Client = Socket.socket (socket.af_inet, socket. SOCK_STREAM)

Client.connect ((Targethost, Targetport))

# Send File size

Client.send (str (fileSize))

Response = CLIENT.RECV (1024)

# Send file name

Client.send (GetFileName (Filefullpath))

Response = CLIENT.RECV (1024)

# Send Thread Count

Client.send (str (threadcount))

Response = CLIENT.RECV (1024)

# Send file Content

For I in Range (ThreadCount):

Filepartsender = Threading. Thread (Target=transferfilesubthread, args= (Filefullpath, i+1, fileSize))

Filepartsender.start ()

For I in Range (ThreadCount):

Sem.acquire ()

Timeend = Time.clock ()

Print "Finished, spent%f seconds"% (Timeend-timestart)


Print "File doesn ' t exist"

def transferfilesubthread (Filefullpath, Threadindex, fileSize):


Client = Socket.socket (socket.af_inet, socket. SOCK_STREAM)

Client.connect ((Targethost, Targetport))

Client.send (str (THREADINDEX))


# Calculate start position and end position

Filepartsize = Filesize/threadcount

StartPosition = Filepartsize * (threadIndex-1)

#print "Thread:%d, startposition:%d"% (Threadindex, startposition)

endposition = filepartsize * threadIndex-1

if Threadindex = = ThreadCount:

Endposition = fileSize-1

Filepartsize = Filesize-startposition

File = open (Filefullpath, "RB") (startposition)

Sentlength = 0

While Sentlength < filepartsize:

Buflen = 1024

Lengthleft = Filepartsize-sentlength

If Lengthleft < 1024:

Buflen = Lengthleft

BUF = (Buflen)

Client.send (BUF)

Sentlength = Len (buf)

File.close ()

Sem.release ()

Print "Part%d finished, size sent%d"% (Threadindex, sentlength)

Except Exception, E:

Print E

Targethost = Raw_input ("Server IP Address:")

targetport = Int (raw_input ("Server Port:")

threadcount = Int (raw_input ("Thread count:")

SEM = threading. Semaphore (0)

While True:

Transferfile ()

Server-side code:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.