Python socket used to transfer large files

Source: Internet
Author: User

The socket recommended Maximum transmission unit is 8,192 characters, but if more than 8192 will cause problems, we can use the following methods to deal with

Client code

Import Subprocessimport Socketip_bind = ("127.0.0.1", 9000) client = Socket.socket () client.connect (ip_bind) L1 = []while True:    option = input ("client:")    Client.sendall (bytes (option,encoding= "Utf-8"))    server_data_size = Client.recv    Print (server_data_size)    a = str (server_data_size,encoding= "Utf-8"). Strip ()    print (a)    L1 = A.split (":")    print (L1)    if l1[0] = = "Server_data_size":        client_size_tmp = l1[1]        print ( CLIENT_SIZE_TMP)        client_size = Int (client_size_tmp)        print (Type (client_size))    client.send (Bytes (" Ok ", encoding=" Utf-8 "))    receive_size = 0    res =" "While    receive_size < client_size:        server_data = CLIENT.RECV (+)        receive_size + = Len (server_data)        res + = str (server_data)    else:        print (RES)        print ("-------reveive down---------")

Service-side code

Import Subprocessimport Socketip_bind = ("127.0.0.1", 9000) Server = Socket.socket () server.bind (ip_bind) Server.listen ( 1) Print ("Server is Waiting ...") Conn,add = Server.accept () print ("Server is connected client") while True:    Client_data = Conn.recv (    a = subprocess). Popen (str (client_data,encoding= "Utf-8"), stdout=subprocess. PIPE)    B = A.stdout.read ()    server_data_size = "server_data_size:" + str (len (b))    Conn.sendall (bytes (server _data_size,encoding= "Utf-8"))    Client_ack = Conn.recv (Ten)    if str (client_ack,encoding= "utf-8") = = "OK":        Conn.sendall (b)

  

Python socket used to transfer large files

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: 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.