Python 我对插座和泡菜有问题。值​;不要在txt中保存到

Python 我对插座和泡菜有问题。值​;不要在txt中保存到,python,sockets,ipc,pickle,Python,Sockets,Ipc,Pickle,思想​​我的Python代码是读取值​​从网络套接字和pickle保存值​​在txt文件中,供以后在其他应用程序中使用 它不一定是一个txt文件,但它是我试图使用的 通信工作得很好,他创建了txt文件,但不幸的是没有记录任何内容 有人可以帮我。 谢谢 服务器代码: 导入套接字 进口泡菜 主机=“” 端口=5000 tcp=socket.socket(socket.AF\u INET,socket.SOCK\u流) orig=(主机、端口) tcp.bind(orig) 听(10) 文件名='da

思想​​我的Python代码是读取值​​从网络套接字和pickle保存值​​在txt文件中,供以后在其他应用程序中使用

它不一定是一个txt文件,但它是我试图使用的

通信工作得很好,他创建了txt文件,但不幸的是没有记录任何内容

有人可以帮我。 谢谢

服务器代码:

导入套接字
进口泡菜
主机=“”
端口=5000
tcp=socket.socket(socket.AF\u INET,socket.SOCK\u流)
orig=(主机、端口)
tcp.bind(orig)
听(10)
文件名='data.txt'
尽管如此:
con,cliente=tcp.accept()
打印('connector by',客户)
尽管如此:
msg=con.recv(4096)
如果没有消息:中断
打印(msg)
将open(filename,'wb')作为f:
腌菜汤(味精,f)
打印('结束客户端连接',客户机)
con.close()
客户端代码:

导入套接字
主机='10.0.0.120'
端口=5000
tcp=socket.socket(socket.AF\u INET,socket.SOCK\u流)
dest=(主机、端口)
tcp.connect(dest)
打印('要退出,请按CTRL+C\n')
msg=input()
而味精\x18':
msg=input()
tcp.sendall(msg.encode('utf8'))
tcp.close()
此处:

while True:
        msg = con.recv(4096)
        if not msg: break
        print(msg)

with open(filename, 'wb') as f:
        pickle.dumps(msg, f)
当且仅当
bool(msg)为False时,才会到达打开文件的代码,因为此时
while True
循环将终止,如下所述:
如果不是msg:break


因此
msg==''
,您最终会写入空字符串您使用了错误的方法<代码>pickle.dumps
生成字符串,不接受文件参数。实际上,您应该从该代码中得到一个异常:

TypeError:需要一个整数(get type\u io.BufferedWriter)

如果您将代码改为使用
pickle.dump
,它可以正常工作,因为这是转储到文件的正确方法。下面的示例演示了它的工作原理(不需要套接字,因为这是关于
pickle
的工作原理,而不是关于网络)

import pickle

foo = b'Some test string'
print("Pickling string '{}'".format(foo))

with open("/tmp/test.pickle", "wb") as tfile:
    pickle.dump(foo, tfile)

with open("/tmp/test.pickle", "rb") as tfile:
    bar = pickle.load(tfile)

print("Reloaded string '{}'".format(bar))
# Confirm they're identical
assert foo == bar