简体   繁体   English

Python:Unix套接字->管道破损

[英]Python: unix socket -> broken pipe

I'm trying to get Python socket working as an alternative to calling the command line socat. 我正在尝试让Python套接字作为调用命令行socat的替代方法。

This socat command works fine: 这个socat命令可以正常工作:

echo 'cmd' | sudo socat stdio <path-to-socket>

but when I run this python code, I get an error: 但是当我运行此python代码时,出现错误:

>>> import socket
>>> s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
>>> s.connect(<path-to-socket>)
>>> s.send('cmd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.error: (32, 'Broken pipe')

Any ideas what the issue is? 任何想法是什么问题? Thanks. 谢谢。

There's one obvious difference between your echo | socat echo | socat之间有一个明显的区别echo | socat echo | socat line and your Python code, and that's the newline that echo adds but you don't send in your Python code. echo | socat行和您的Python代码,这就是echo添加的换行符,但您没有发送Python代码。 I don't see how that could cause the broken pipe error. 我看不到这怎么会导致管道破裂错误。 Your basic approach seems fine, and it works fine when I test locally. 您的基本方法看起来不错,当我在本地测试时也可以正常工作。 Are you sure the server process isn't doing something extra here? 您确定服务器进程在这里没有做额外的事情吗? Are you sure it's still listening, and hasn't, for example, closed the other end of the socket for some reason? 您确定它仍在监听,例如由于某种原因没有关闭插槽的另一端?

I was having this problem, and it ended up being that my code worked fine when in a script - but got a broken pipe when working interactively. 我遇到了这个问题,最终导致我的代码在脚本中运行良好-但是在交互工作时管道破裂。 I guess there's a timeout or something. 我想可能是超时或其他原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM