Upload and download based on TCP protocol

Source: Internet
Author: User
Tags unpack

Directory format:

Build this directory to be free to use!

Client Side

Import socketimport sysimport osimport jsonimport Structsk = Socket.socket () adress = (' 192.168.12.11 ', 29231) def upload ()    : message = {' filename ': none, ' FileSize ': none} sk.connect (adress) temp = ' upload ' sk.send (temp.encode (' Utf-8 ')) Meg = input (' Input file path: ') filename = Os.path.basename (meg) filesize = Os.path.getsize (meg) message[' filename '] = FileName message[' filesize '] = filesize str_message = json.dumps (message) Len_message = Len (str_message) B_len _message = Struct.pack (' i ', len_message) sk.send (b_len_message + str_message.encode (' Utf-8 ')) with open (Meg, ' RB ') a s F:while filesize:connent = F.read () sk.send (connent) FileSize-= Len (connent) d EF Download (): Path = Os.path.dirname (Os.path.abspath (__file__)) sk.connect (adress) temp = ' Download ' Sk.send (TEMP.E Ncode (' utf-8 ')) num = Sk.recv (1024x768). Decode (' Utf-8 ') print (' Downloadable file:%s '% (num,)) name = input (' Enter download file name: ') sk.se nd (Name.encode (' Utf-8 '))   Path = '/'. Join ([path, ' local file ', name]) B_len_message = SK.RECV (4) len_message = Struct.unpack (' i ', b_len_message) [ 0] res_s = SK.RECV (len_message). Decode (' utf-8 ') str_message = Json.loads (res_s) filesize = str_message[' FileSize '            ] with open (path, ' AB ') as f:while filesize:connent = Sk.recv (1024x768) f.write (connent) F.flush () FileSize-= Len (connent) def main (): dic = [(' Upload ', ' upload '), (' Download ', ' Downlo Ad ')] for I in enumerate (DIC, 1): Print (i[0], i[1][0]) num = int (input (' Enter option: ')) GetAttr (sys.modules[ __NAME__], dic[num-1][1]) () sk.close () if __name__ = = ' __main__ ': Main ()

Server Side

Import Socketimport structimport Osimport Jsonpath = Os.path.abspath (__file__) path = os.path.dirname (path) SK = Socket.socket () adress = (' 0.0.0.0 ', 29231) sk.bind (adress) Sk.listen (4) conn, addr = sk.accept () def upload (path): B_len_m Essage = CONN.RECV (4) len_message = Struct.unpack (' i ', b_len_message) [0] res_s = CONN.RECV (len_message). Decode (' utf-    8 ') Str_message = json.loads (res_s) filename = ' 1 ' + str_message[' filename '] filesize = str_message[' filesize ']  Path = '/'. Join ([path, ' client uploaded file ', filename]) with open (path, ' AB ') as f:while filesize:connent =    CONN.RECV (1024x768) f.write (connent) F.flush () filesize-= Len (connent) def download (path):  message = {' filename ': none, ' FileSize ': none} res = Os.listdir ('/'. Join ([path, ' client uploaded file ')]) sum = ' For i '    res:sum = sum + ' + i + ' conn.send (Sum.encode (' utf-8 ')) filename = Conn.recv (1024x768). Decode (' Utf-8 ') Path = '/'. Join ([path, ' Client-uploaded file '), filename]) filesize = os.path.getsize (path) message[' filename '] = path message[' filesize '] = filesize str_me Ssage = json.dumps (message) Len_message = Len (str_message) b_len_message = Struct.pack (' i ', len_message) Conn.sen D (b_len_message + str_message.encode (' Utf-8 ')) with open (path, ' RB ') as f:while filesize:connent = F.read () conn.send (connent) FileSize-= Len (connent) def main (): res_s = Conn.recv (1024x768). Decode (' U Tf-8 ') if res_s = = ' upload ': Upload (path) sk.close () If res_s = ' Download ': Download (path) Sk.clo SE () if __name__ = = ' __main__ ': While 1:main ()

Upload and download based on TCP protocol

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: info-contact@alibabacloud.com 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.