代码拉取完成,页面将自动刷新
# -- coding: GBK --
import socket
import time
import base64
def sendfile(clientsocket, filename):
with open(filename, 'rb') as f:
data = f.read()
clientsocket.sendall(data)
print("文件发送完成")
clientsocket.sendall(b"EOF")
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serveraddress = ('localhost', 50400)
clientsocket.connect(serveraddress)
print(f'已连接到服务器 : {serveraddress}')
try:
while True:
action = input("请输入操作(\"send\"发送文件,“msg”发送消息):").strip().lower()
if action == "send":
filename = input("请输入要发送的文件名:")
clientsocket.sendall(f"FILE:{filename}".encode('GBK'))
print(f"正在发送文件{filename}...")
time.sleep(1)
sendfile(clientsocket, filename)
confirmation = clientsocket.recv(1024).decode('GBK')
if confirmation == "FILERECEIVED":
print("文件传输成功。")
elif action == "msg":
message = input("请输入要发送的消息:")
clientsocket.sendall(message.encode('GBK'))
response = clientsocket.recv(1024)
response_base64_decoded = base64.b64decode(response)
response_decoded = response_base64_decoded.decode('GBK')
print(f"收到服务端回应:{response_decoded}")
else:
print("未知操作。")
if input("是否要继续?Y/N: ").strip().upper() != "Y":
break
except Exception as e:
print(f"发生错误:{e}")
finally:
clientsocket.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。