Python.将标准输出重定向到套接字
我在计算机A"上运行我的脚本.然后我连接到计算机A"来自计算机B"通过我的脚本.我将消息发送到计算机A"我的脚本使用 exec()
指令运行它.
I run my script on computer "A". Then I connect to computer "A" from computer "B" through my script. I send my message to computer "A" and my script runs it with an exec()
instruction.
我想通过计算机B"上的套接字查看在计算机A"上执行我的消息的结果.
I want to see the result of executing my message on computer "A", through a socket on computer "B".
我尝试更改 sys.stdout = socket_response
但出现错误:Socket object has no attribute write()"
I tried to change sys.stdout = socket_response
but had a error: "Socket object has no attribute write()"
那么,如何从计算机A"重定向标准输出(用于 print
或 exec()
)?到计算机B"通过套接字连接?
So, how can I redirect standard output (for print
or exec()
) from computer "A" to computer "B" through socket connection?
这将是我的脚本中的某种python 解释器".
It will be some kind of 'python interpreter' into my script.
抱歉,我无法在没有声誉的情况下回答我自己的问题
谢谢大家!
我使用了@Torxed 建议我使用的简单方法.这是我的伪代码(这只是一个例子,不是我真正的脚本)
I use a simple way, which @Torxed advised me of. Here's my pseudo-code (it's just an example, not my real script)
#-*-coding:utf-8-*-
import socket
import sys
class stdout_():
def __init__(self, sock_resp):
self.sock_resp = sock_resp
def write(self, mes):
self.sock_resp.send(mes)
MY_IP = 'localhost'
MY_PORT = 31337
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Start server")
old_out = sys.stdout
srv.bind((MY_IP, MY_PORT))
srv.listen(0)
sock_resp, addr_resp = srv.accept()
new_out = stdout_(sock_resp)
sys.stdout = new_out
#sys.stdout = sock_resp ### sock_object has no attribute 'write'
while 1:
try:
a = sock_resp.recv(1024)
exec(a)
except socket.timeout:
#print('server timeout!!' + '\n')
continue
我使用 Putty 连接到脚本并发送了print 'abc'";然后我收到了答案abc".
I connected to script with Putty and sent "print 'abc'" and then I received the answer 'abc'.
有 makefile
Python 套接字类中的函数:
There is the makefile
function in Python's socket class:
socket.makefile(mode='r', buffering=None, *, encoding=None,错误=无,换行符=无)
socket.makefile(mode='r', buffering=None, *, encoding=None, errors=None, newline=None)
返回与套接字关联的文件对象.准确返回类型取决于提供给 makefile() 的参数.这些论点是与内置 open() 函数的解释方式相同.
Return a file object associated with the socket. The exact returned type depends on the arguments given to makefile(). These arguments are interpreted the same way as by the built-in open() function.
关闭文件对象不会关闭套接字,除非没有对套接字的剩余引用.套接字必须处于阻塞状态模式;它可以有一个超时,但文件对象的内部缓冲区可能如果发生超时,最终会处于不一致的状态.
Closing the file object won’t close the socket unless there are no remaining references to the socket. The socket must be in blocking mode; it can have a timeout, but the file object’s internal buffer may end up in a inconsistent state if a timeout occurs.
您可以在 Mark Lutz 的书中阅读如何使用它(第 12 章,使套接字看起来像文件和流").
You can read how to use it in Mark Lutz's book (chapter 12, "Making Sockets Look Like Files and Streams").
书中的一个例子(想法很简单:使用 socket.makefile
从套接字创建一个文件对象并将 sys.stdout
与它链接):>
An example from the book (the idea is simple: make a file object from a socket with socket.makefile
and link sys.stdout
with it):
def redirectOut(port=port, host=host):
"""
connect caller's standard output stream to a socket for GUI to listen
start caller after listener started, else connect fails before accept
"""
sock = socket(AF_INET, SOCK_STREAM)
sock.connect((host, port)) # caller operates in client mode
file = sock.makefile('w') # file interface: text, buffered
sys.stdout = file # make prints go to sock.send
return sock # if caller needs to access it raw