1 Star 0 Fork 0

兰风/python

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Client2025.py 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
w317714 提交于 2025-04-22 21:02 +08:00 . 写完了
# -- 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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/w317714/py-meow-meow-meow-meow.git
git@gitee.com:w317714/py-meow-meow-meow-meow.git
w317714
py-meow-meow-meow-meow
python
master

搜索帮助