You need to enable JavaScript to run this app. Python socket开发类Steam平台zip文件传输 服务端客户端WinError解决
优惠活动
大模型
产品
解决方案
定价
更多

Python socket开发类Steam平台zip文件传输 服务端客户端WinError解决

问题排查与解决方案

连接阻断类WinError快速修复方法

  • 不要用socket.gethostname()做绑定/连接地址:主机名默认会解析为非环回的局域网地址或IPv6地址,Windows安全策略大概率会拦截未签名程序的非环回端口监听。本地测试时服务端绑定地址填0.0.0.0、客户端连接地址填127.0.0.1;跨设备传输时客户端填服务端设备的实际局域网IPv4地址即可。
  • 端口占用排查:按Win+R输入cmd打开命令提示符,执行netstat -ano | findstr :5000,如果有返回结果,杀掉对应PID的进程,或者换用1024以上的未被占用端口(如50001)。
  • 补充Python解释器防火墙规则:不要仅关闭防火墙,要在Windows高级防火墙设置中,给你当前使用的python.exe(项目虚拟环境或系统Python安装目录下的对应解释器)分别添加入站、出站允许规则,开放所有网络类型的权限。

代码逻辑修复(现有代码缩进错误会直接导致收发失败)

修正后客户端代码

import socket
import tqdm
import os
import time

# 本地测试填127.0.0.1,跨设备填服务端局域网IPv4
SERVER_HOST = "127.0.0.1"
SERVER_PORT = 5000

BUFFER_SIZE = 4096
SEPARATOR = "<SEPARATOR>"
gamename = "gra" 

s = socket.socket()
s.connect((SERVER_HOST, SERVER_PORT))
print("成功连接到服务器")
s.send(gamename.encode("utf-8"))
print("游戏下载请求已发送")
received = s.recv(BUFFER_SIZE).decode("utf-8")
print(received)
try:
    filename, filesize = received.split(SEPARATOR)
except:
    raise ValueError("收到的文件信息格式错误")
filename = "effect.zip"
filename = os.path.basename(filename)
print(filename)
filesize = int(filesize)
progress = tqdm.tqdm(range(filesize), f"接收 {filename}", unit="B", 
unit_scale=True, unit_divisor=1024)
with open(filename, "wb") as f:
    while True:
        bytes_read = s.recv(BUFFER_SIZE)
        if not bytes_read:    
            break
        f.write(bytes_read)
        progress.update(len(bytes_read))
s.close()

修正后服务端代码

import socket
import tqdm
import os
import time

SEPARATOR = "<SEPARATOR>"
BUFFER_SIZE = 4096 

def send_file(host, port, buffer):
    s = socket.socket()
    # 开启地址复用,避免端口占用报错
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    print("启动服务中")
    s.bind((host, port))
    s.listen(5)
    print(f"已在端口 {port} 启动监听")
    while True:
        client_socket, address = s.accept()
        print(f"客户端 {address} 已连接")
        time.sleep(0.5)
        received = client_socket.recv(buffer).decode("utf-8")
        filename = received + ".zip"
        # 先判断文件是否存在,避免无提示崩溃
        if not os.path.exists(filename):
            client_socket.send(b"ERROR: File not found")
            client_socket.close()
            continue
        print("收到安装请求")
        filesize = os.path.getsize(filename)
        print(f"{filename}{SEPARATOR}{filesize}")
        client_socket.send(f"{filename}{SEPARATOR}{filesize}".encode("utf-8"))
        progress = tqdm.tqdm(range(filesize), f"发送 {filename}", unit="B", unit_scale=True, unit_divisor=1024)
        with open(filename, "rb") as f:
            while True:
                bytes_read = f.read(BUFFER_SIZE)
                if not bytes_read:
                    break
                client_socket.sendall(bytes_read)
                progress.update(len(bytes_read))
        client_socket.close()
    s.close()
# 服务端绑定0.0.0.0监听所有网卡地址
host = "0.0.0.0"
send_file(host, 5000, BUFFER_SIZE)

内容的提问来源于stack exchange,提问作者ChrisFromNowhere

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 03:15:03