【问题标题】:How to print data on client side using Python如何使用 Python 在客户端打印数据
【发布时间】:2021-12-26 09:28:54
【问题描述】:

我正在创建一个日志观察器来模拟 Linux 中的 tail-f 功能。 所以,基本上这就是我想做的事情

  1. 编写可以检测日志文件中发生的更改的服务器代码。
  2. 如果在日志文件中检测到更改,服务器应将数据发送到客户端,客户端应打印数据。

但这是发生了什么

  1. 服务器上的代码能够检测到日志文件中发生的变化。
  2. 但更改仅在服务器端打印,即使我将服务器日志发送到客户端
  3. 客户端没有打印任何内容。

这是我的代码 服务器.py

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1243))
s.listen(5)

class Tail():
    def __init__(self,file_name,callback=sys.stdout.write):
        self.file_name = file_name
        self.callback = callback
    def follow(self,n):
        try:
            with open(self.file_name) as f:
                self._file = f
                self.showLastLine(n)
                self._file.seek(0,2)
                while True:
                    line = self._file.readline()
                    if line:
                        self.callback(line)
        except Exception as e:
            print('Failed to open the file. See if the file does not exist or if there is a problem with permissions')
            print(e)
    
    def showLastLine(self, n):
        last_lines = self._file.readlines()[-10:]
        for line in last_lines:
            self.callback(line)

py_tail = Tail('log.log')
while True:
    clientsocket, address = s.accept()
    clientsocket.send(bytes(py_tail.follow(10),"utf-8"))

client.py

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1243))

while True:
    msg = s.recv(16)
    msg.decode("utf-8")
    print(msg)

现在我刚刚创建了一个虚拟的log.log 文件,看起来像这样

1
2
3
4
5

所以当我编辑日志文件并输入时

6
7
8

并保存它,更改会反映在我的 server.py 文件的输出中,但我希望输出显示在我的 client.py 文件中,这不会发生。

我只是在探索套接字编程,由于我是新手,这可能是一个愚蠢的问题,但我真的需要帮助。

【问题讨论】:

  • 你没有在跟随函数中返回或产生任何东西。所以没有任何东西从服务器发送到客户端。
  • 我也试过返回,但没有帮助。你能帮我解决一下吗?

标签: python websocket client-server


【解决方案1】:

我已经相应地更改了代码,这就是我可以修复它的方法

server.py

import socket
import time
import sys
import os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1243))
s.listen(5)

def follow(thefile):
    '''generator function that yields new lines in a file
    '''
    # seek the end of the file
    thefile.seek(0, os.SEEK_END)
    
    # start infinite loop
    while True:
        # read last line of file
        line = thefile.readline()
        # sleep if file hasn't been updated
        if not line:
            time.sleep(0.1)
            continue

        yield line

logfile = open("log.log","r")
loglines = follow(logfile)

while True:
    clientsocket, address = s.accept()
    for line in loglines:
        clientsocket.send(bytes(line,"utf-8"))

client.py

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1243))

while True:
    lines = s.recv(16)
    lines=lines.decode("utf-8")
    print(lines)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-04
    • 2016-10-27
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多