python 3.5 implements file transfer based on TCP
- 2020-11-20 06:09:40
- OfStack
This article shares the specific code of python3.5 file transfer based on TCP for your reference. The specific content is as follows
Server code
# _*_ coding:utf-8 _*_
from socket import *
import _thread
def tcplink(skt,addr):
print(skt)
print(addr," It's hooked up ...")
print(' Start sending files ')
with open('./ww.jpg', 'rb') as f:
for data in f:
print(data)
skt.send(data)
f.close()
skt.close()
HOST = "127.0.0.1"
PORT = 23333
ADDR = (HOST,PORT)
server = socket(AF_INET,SOCK_STREAM)
server.bind(ADDR)
server.listen(5)
while True:
print(" Waiting for the connection ...")
skt,addr = server.accept()
print(skt)
try:
_thread.start_new_thread(tcplink,(skt,addr))
except:
print(" Thread cannot start ")
server.close()
Client code
# _*_ utf-8 _*_
from socket import *
HOST = "127.0.0.1"
PORT = 23333
ADDR = (HOST,PORT)
client = socket(AF_INET,SOCK_STREAM)
client.connect(ADDR)
with open("./gg.jpg","ab") as f:
while True:
data = client.recv(1024)
if not data:
break;
f.write(data)
f.close()
print(" After receiving ")
client.close()
The above code has been tested and worked well, so hopefully it will inspire you.