我会使用telnetlib.Telnet.expect()
来实现,类似于这样:
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])