python将telnet链接输出的文件,在python中访问telnet会话

so I need to access a telnet session. More specifically JPL's ephemeris service. I know exactly what I need to do in the command prompt, but I've had trouble using the telnetlib package.

Here are the steps I need to take through command prompt:

telnet

o horizons.jpl.nasa.gov 6775

DES=C/2012 X1;

y

E

o

H06

y

2013-Nov-7 9:00

2013-Nov-17 9:00

1d

y

1,4,9,19,20,24

and then after that there is a large output that I need to save to a text file, or simply keep as a variable. I'll be using it later.

And following these inputs step by step should get you to the exact bit of information I need to grab

Any advice?

解决方案

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])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值