0

Program:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '101.72.14.20'
port = 23
s.connect((host, port))
tm = s.recv(1024)
s.close()
print("The time got from the server is %s" % tm.decode('ascii'))

Error:

$ python cli.py
Traceback (most recent call last):
  File "cli.py", line 20, in <module>
     print("The time got from the server is %s" % tm.decode('ascii'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
$

It is a simple client program. I tried to read the content from server. It successfully reads. But it shows an error while printing as "UnicodeDecodeError". Can anyone help me to solve this problem.

Note:

101.72.14.20 is a mainframe server. I expect the data is in the form of EBCDIC
mohangraj
  • 6,634
  • 12
  • 44
  • 75
  • @TessellatingHeckler I just convert it to ascii. Is it right? – mohangraj May 31 '17 at 05:43
  • `print("The time got from the server is %s" % tm.decode('cp500'))` . Still it shows some junk characters like `The time got from the server is ?Ù` – mohangraj May 31 '17 at 05:49
  • what will be if you just print tm? – Tiny.D May 31 '17 at 05:53
  • @Tiny.D It shows like this `The time got from the server is ÿý` – mohangraj May 31 '17 at 05:55
  • Clearly the characters are not in ASCII range. Now when you do tm.decode("ascii"), You are telling to decode the string to unicode using ascii encoding but clearly It's not possible here. – Ishan Bhatt May 31 '17 at 06:16
  • it seems that tm is already in the form of EBCDIC, no need to decode. – Tiny.D May 31 '17 at 06:17
  • @IshanBhatt Is there any way to print it as ASCII – mohangraj May 31 '17 at 06:33
  • @mrg I am afraid there is no way. You can think of a regex that completely removes non ascii and replaces it with whatever you want. https://stackoverflow.com/questions/20078816/replace-non-ascii-characters-with-a-single-space – Ishan Bhatt May 31 '17 at 06:38
  • If you think the data is EBCDIC then you might try `decode('cp037')` - but I don't think this will help as `0xff` isn't a valid EBCDIC-character either. – piet.t May 31 '17 at 07:06
  • What service listens on port 23 - telnet or TN3270? It makes a difference, as telnet traffic from a mainframe will be in **ASCII**, not EBCDIC. If port 23 is TN3270, I can expect X'FF' in the data stream, since it's 3270; see RFCs [1576](https://tools.ietf.org/html/rfc1576), [1646](https://tools.ietf.org/html/rfc1646), and [1647](https://tools.ietf.org/html/rfc1647). Text transmitted in a 3270 data block will be in EBCDIC, but protocol commands are transmitted in ASCII. If port 23 traffic is TN3270, common telnet ports are 623 or 1023. Print the hex value of `tm`, please. – zarchasmpgmr Jun 01 '17 at 20:24

0 Answers0