0

I'm new to this site and obviously also to python. I need to send to a telnet connection a command as following: FF€ (followed by a euro sign and a return)

(Note: the FF shall be ASCII and the euro sign different to ASCII--> unicode?) How can I send this? The code is following:

import getpass
import sys
import telnetlib

HOST = "127.0.0.1"
port = "11000"

tn = telnetlib.Telnet(HOST, port)
tn.write('FF\x80')
print(tn.read_all())

Many thanks! Haki

1 Answers1

0

That depends on what the remote system is expecting from you. There are about 10 different ways to encode the euro sign depending on the charset on the remote side.

You will have to ask someone who understands the remote system to tell you which encoding it expects.

After that, you can use the Unicode symbol \u20AC and the codecs module to convert that into the charset which the remote side expects.

Note that you will want to use Unicode strings as will if you still use Python 2 (u'...').

[EDIT] It seems the remote side is Windows or uses the Windows charset cp1252 (see this question)

So this should work:

t=u'FF\u20acFF\u20ac\r\n'
e=t.encode('cp1252')
tn.write(e)

response = tn.read_all().decode('cp1252')
print(response)

Note that you have to encode anything you send and decode anything you receive.

Also note: On Windows, the line end is \r\n. Most programs don't work correctly if you send the wrong line ending.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Many thanks. The server side is expecting a "Terminal font" 80Hex for which the ASCII representation of this is the € sign. – user3437893 Mar 19 '14 at 14:22
  • Ok here is what I have got, still not working: Would anybody be so kindly to help me. I am already thinking using alternative's. Many thanks in advance! import getpass import sys import telnetlib import sys print sys.getdefaultencoding() reload(sys) sys.setdefaultencoding("UTF-8") print sys.getdefaultencoding() HOST = "127.0.0.1" port = "11000" tn = telnetlib.Telnet(HOST, port) t=u'FF\u20acFF\u20ac\r' e=t.encode('utf8') d=e.decode('utf8') tn.write(d) print(tn.read_all()) – user3437893 Mar 19 '14 at 17:33
  • Many thanks it does not seem to help. I have tried with a C++ program and this helped. Unfortunately, only if I use a windows machine connecting the device, i.e. linux gives failure, but this is another problem now. – user3437893 Apr 02 '14 at 09:06