Accessing a telnet session in python

半腔热情 提交于 2019-11-29 05:08:54

I would use telnetlib.Telnet.expect(), something like this:

import telnetlib

t = telnetlib.Telnet()
t.open('horizons.jpl.nasa.gov', 6775)

expect = ( ( r'Horizons>', 'DES=C/2012 X1\n' ),
           ( r'Continue.*:', 'y\n' ),
           ( r'Select.*E.phemeris.*:', 'E\n'),
           ( r'Observe.*:', 'o\n' ),
           ( r'Coordinate center.*:', 'H06\n' ),
           ( r'Confirm selected station.*>', 'y\n'),
           ( r'Accept default output.*:', 'y\n'),
           ( r'Starting *UT.* :', '2013-Nov-7 09:00\n' ),
           ( r'Ending *UT.* :', '2013-Nov-17 09:00\n' ),
           ( r'Output interval.*:', '1d\n' ),
           ( r'Select table quant.* :', '1,4,9,19,20,24\n' ),
           ( r'Scroll . Page: .*%', ' '),
           ( r'Select\.\.\. .A.gain.* :', 'X\n' )
)

with open('results.txt', 'w') as fp:
    while True:
        try:
            answer = t.expect(list(i[0] for i in expect), 10)
        except EOFError:
            break
        fp.write(answer[2])
        fp.flush()
        t.write(expect[answer[0]][1])

Between April and May 2013, some people actually wrote an entire Python package around JPL's Horizons Telnet interface. It can be found here:

https://pypi.python.org/pypi/HorizonJPL

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!