sam*_*sam 4 python command-line tcp send
我想在python中开发一个代码,它将在localhost中打开一个端口,并将日志发送到该端口.日志只是python文件的命令输出.
喜欢 :
hello.py
i = 0
while True:
print "hello printed %s times." % i
i+=1
Run Code Online (Sandbox Code Playgroud)
这将不断打印声明.我想要这个续.输出发送到打开的端口.
任何人都可以告诉我如何做同样的事情?
提前致谢
pas*_*ean 14
这就是我想出的.
与您的脚本一起使用:
hello.py | thisscript.py
Run Code Online (Sandbox Code Playgroud)
希望这是你想要的.
import socket
import sys
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
while True:
line = sys.stdin.readline()
if line:
s.send(line)
else:
break
s.close()
Run Code Online (Sandbox Code Playgroud)
这可以扩展为使用argv指定端口