Server
import socket
IP = '10.7.181.90'
PORT = 12345
if __name__ == '__main__':
server = socket.socket()
server.bind((IP, PORT))
server.listen(50)
while True:
conversation, addr = server.accept()
while True:
msg_send = input('>>>')
conversation.send(msg_send.encode())
if isinstance(msg_send, bytes):
with open('./serverSendFile', 'rb') as f:
data_send = f.read()
conversation.send(data_send)
msg_recv = conversation.recv(1024)
if msg_recv == '拜拜':
conversation.close()
else:
print(msg_recv.decode())
data = bytes()
if isinstance(msg_recv, bytes):
while msg_recv:
data += msg_recv
msg_recv = conversation.recv(1024)
with open('./serverRecvFile', 'wb') as f:
f.write(data)
conversation.close()
Client
import socket
IP = '10.7.181.90'
PORT = 12345
if __name__ == '__main__':
client = socket.socket()
client.connect((IP, PORT))
while True:
data_recv = client.recv(1024)
data = bytes()
msg_send = input('>>>')
if isinstance(msg_send, bytes):
with open('./serverSendFile', 'rb') as f:
data_send = f.read()
client.send(data_send)
if isinstance(data_recv, bytes):
while data_recv:
data += data_recv
data_recv = client.recv(1024)
with open('./clientRecvFile', 'wb') as f:
f.write(data)
else:
print(data_recv.decode())
if data_recv == '拜拜':
client.close()
client.close()
网友评论