简体   繁体   English

Python telnetlib连接到Scrapy Telnet以读取统计信息

[英]Python telnetlib to connect to Scrapy Telnet to read stats

I have a Scrapy spider running for days. 我有一头Scrapy蜘蛛跑了好几天。 I usually try to check stats as how many items it has scraped and so. 我通常会尝试检查统计数据,其中包括已刮除的项目数等等。 I simply run following cmds in terminal and it gives me stats. 我只是在终端中跟随cmd运行,它为我提供了统计信息。

$ telnet [IP] [PORT]
>>> spider.name
alf-spider
>>> stats.get_stats()
...

Now I want to do this with Python using telnetlib but I can't achieve above results. 现在,我想使用telnetlib在Python中执行此操作,但是我无法实现上述结果。 Following is my python code. 以下是我的python代码。

#!/usr/bin/env python

import sys
import getpass
import telnetlib

HOST = "192.168.1.5"

def main():
    ports = ['6023']
    if len(sys.argv) > 1:
        ports = sys.argv[1].split(',')

    for port in ports:
        get_stats(port)

def get_stats(port):
    tn = telnetlib.Telnet(HOST, port)
    tn.write("spider.name\n")
    print tn.read_all()

if __name__ == '__main__':
    main()

Above code if ran just hangs until force closed. 上面的代码如果运行只是挂起,直到强制关闭。 What am I missing? 我想念什么?

This will work. 这将起作用。

#!/usr/bin/env python

import sys
import getpass
import telnetlib

HOST = "192.168.1.5"

def main():
    ports = ['6023']
    if len(sys.argv) > 1:
        ports = sys.argv[1].split(',')

    for port in ports:
        get_stats(port)

def get_stats(port):
    tn = telnetlib.Telnet(HOST, port)
    tn.read_until('>>>')
    tn.write("spider.name\n")
    print tn.read_until('>>>')
    tn.close()

if __name__ == '__main__':
    main()

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

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