Skip to content

Socket

socket 简单文件传输

https://github.com/x4nth055/pythoncode-tutorials/tree/master/general/transfer-files

file_send.py
import socket

BUFFER_SIZE = 1024 * 1024
sk = socket.socket()

sk.connect(('192.168.19.40', 9999))

with open("C:\\Users\\62497\\Downloads\\VSCodeUserSetup-x64-1.91.1.exe", "rb") as f:
    while True:
       d = f.read(BUFFER_SIZE)
       if not d:
           break
       sk.sendall(d)
sk.send('quit'.encode())
file_recv.py
import socket

BUFFER_SIZE = 1024 * 1024

sk = socket.socket()

sk.bind(('192.168.19.40', 9999))

sk.listen(5)

conn, address = sk.accept()

while True:
    with open('file', "ab") as f:
        data = conn.recv(BUFFER_SIZE)
        print(len(data))
        if not data:
            break
        f.write(data)
sk.close()