Telnetlib: Not receiving anything - python

I have a question regarding Python`s telnetlib-Library.
import telnetlib
message= ('\x02'+'DD'+'\x03')
print message
tn = telnetlib.Telnet('IP','PORT')
tn.write(message)
while True:
data = tn.read_all()
if data:
print data
I want to communicate with a data-logger. The data-logger expects commands which looks like this:
STX + command + ETX
By sending the STX+command+ETX-phrase via Putty / telnet to the data-logger, the data logger "answers", e.g. by sending the current time.
Now, I want to send the same thing via python script to the logger. Unfortuantely, I do not receive any answer in the python shell, just a white space.
Can you please help me?
Regard,
Phil

Using doblequote for special character code
message= ("\x02"+'DD'+"\x03")
Have u already declare IP address and port number.
tn = telnetlib.Telnet('192.168.0.10',2021)

Related

Is there some special encoding required when sending ASCII commands over Serial (USB-RS232) port in Python3.8?

Firstly, I have also researched an seen that this question is answered alot of places, but the answers don't work for me.
I am trying to send ASCII command over serial port to a PCBA and then attempting to receive the response.
HW setup:
Computer
USB-RS232 Cable
PCBA
SW:
Python 3.8
I have sent commands over RS232 terminals (realterm) and they work perfectly (LED turns on) but can't implement the same with python. The command in question is "led_r_on".
And yes, I get "COM5 is open" prompt, meaning COM5 is alive.
Thanks for helps!
code as follows:
import serial
import time
from time import sleep
from serial import Serial
port = 'COM5'
baud = 115200
ser = serial.Serial(port, baud, timeout=2)
if ser.isOpen():
print(ser.name + ' is open...')
sleep(1)
ser.write(b'led_r_on')
print(ser.readline(1000))
Since you didn't give any information on the pcba you are using. I will just assume, that you are missing some kind of end delimiter in your bytesequence. Now there are many ways of ending a transmission over serial. The most frequent ones I ran across are these two:b'led_r_on\r\n' or just b'led_r_on\n'. Try changing your bytes in the ser.write() function to this. If that does not work you might need to use this: b'led_r_on\x04' this will send the EOT (End of Transmission) ASCII character. But I don't think this will be needed.

How to Send "ESC+3" and "ESC+E" in Python telnetlib?

How to Send "ESC+3" and "ESC+E" in Python telnetlib.
Code:
tn = telnetlib.Telnet(host)
tn.write(ESC+3)
Now what I have to give in the place of ESC+3
You can found the ASCII code from:
When found the code, write the data.
tn.write("\x1B\x33")

I am receiving \\r\\n on carriage return instead of \r\n

The project uses sockets to read from a connecting client until either there is no more characters to be read or it receives a \r\n. Here is a snippet of the code:
while True:
ch = connection.recv(1)
data += ch.decode('UTF-8')
if data.endswith('\r\n') or not ch:
data = data.replace('\r\n','')
break
The code works as intended when windows is used to run the server that reads from clients. However when I try to run it on a raspberry pi running rasbian, it always reads a carriage return as '\\r\\n'. For example when a client sends:
-list_networks wlan0 5180<return>
yields a string looking like:
-list_networks wlan0 5180\\r\\n
Why is this? Because of this it does not get read as a carriage return and is missed. I know different OS return different of strings for carriage return but I didn't find anything about this string when I researched a bit. Am I missing something? Suggestions and explanations are appreciated.
edit:
Forgot to add the command is sent through a telnet connection. I want to be able to connect to the socket via telnet. Type a command and when the enter key is pressed the command the loop will recognize and end the loop.

Python and telnetlib on 2.7. Totally not working for me

Below is the streaming data that i get when I connect from a command line. Yet..quit shocking...I get no data from from python. Why cant I print to screen the output? One would think I should just connect to to server, ready very eager.
tn = telnetlib.Telnet()
tn.open(HOST, TRADE_PORT)
while True:
print '-',tn.read_very_eager()
-
-
-
-
Below us a working telnet session on same machine.
ubuntu#ubuntu:~$ telnet 222.222.222.222 2000
Trying 222.222.222.222...
Connected to 222.222.222.222.
Escape character is '^]'.
S=SXL,P=157.00,Q=3221,T=130621,N=40111759,E=371993|S=SXL,P=157.00,Q=45,T=130621,N=40111760,E=371994|S=SXL,P=157.00,Q=1401,T=130621,N=40111761,E=371995|S=SXL,P=157.00,Q=46,T=130621,N=40111762,E=371996|S=SXL,P=157.00,Q=37,T=130621,N=40111763,E=371997|S=SXL,P=157.00,Q=60,T=130621,N=40111764,E=371998|S=RIO,P=6514.50,Q=57,T=130621,N=10085304,E=371999|S=SXL,P=157.00,Q=39,T=130621,N=40111765,E=372000|S=SXL,P=157.00,Q=701,T=130621,N=40111766,E=372001|S=SXL,P=157.00,Q=119,T=130621,N=40111767,E=372002|S=SXL,P=157.00,Q=120,T=130621,N=40111768,E=372003|S=SXL,P=157.00,Q=47,T=130621,N=40111769,E=372004|S=SXL,P=157.00,Q=22,T=130621,N=40111770,E=372005,C=XT|S=SXL,P=157.00,Q=98,T=130621,N=40111771,E=372006|S=SXL,P=157.00,Q=924,T=130621,N=40111772,E=372007|S=SXL,P=157.00,Q=381,T=130621,N=40111773,E=372008|S=SXL,P=157.00,Q=221,T=130621,N=40111774,E=372009|S=SXL,P=157.00,Q=220,T=130621,N=40111775,E=372010|S=SXL,P=157.00,Q=313,T=130621,N=40111776,E=372011|S=RIO,P=6514.00,Q=61,T=130621,N=10085305,E=372012|S=RIO,P=6514.00,Q=32,T=130621,N=10085306,E=372013|S=PMV,P=762.00,Q=17,T=130621,N=40111777,E=372014|S=RIO,P=6514.00,Q=8,T=130621,N=10085307,E=372015|S=RIO,P=6514.00,Q=18,T=130621,N=10085308,E=372016|S=RIO,P=6514.00,Q=65,T=130621,N=10085309,E=372017|S=AMP,P=456.50,Q=129,T=130621,N=20091811,E=372018|S=AMP,P=456.50,Q=482,T=130621,N=20091812,E=372019|S=GPT,P=353.50,Q=109,T=130621,N=20091813,E=372020|S=RSG,P=47.50,Q=194,T=130621,N=10085310,E=372021|S=AGK,P=1499.00,Q=147,T=130621,N=40111778,E=372022|S=AGK,P=1499.00,Q=183,T=130621,N=40111779,E=372023|S=AGK,P=1499.00,Q=1,T=130621,N=40111780,E=372024|S=AGK,P=1499.00,Q=183,T=130621,N=40111781,E=372025|S=AGK,P=1499.00,Q=29,T=130621,N=40111782,E=372026|S=AGK,P=1499.00,Q=77,T=130621,N=40111783,E=372027|S=AGK,P=1499.00,Q=21,T=130621,N=40111784,E=372028|S=AGK,P=1499.00,Q=492,T=130621,N=40111785,E=372029|S=AGK,P=1498.50,Q=68,T=130621,N=40111786,E=372030|S=AGK,P=1499.00,Q=7,T=130621,N=40111787,E=372031|S=AGK,P=1499.00,Q=25,T=130621,N=40111788,E=372032|S=SYDDA,P=388.00,Q=50,T=130621,N=30083122,E=372033|S=PCL,P=6.10,Q=145,T=130621,N=30083123,E=372034|S=VAS,P=6727.00,Q=744,T=130622,N=30083124,E=372035|S=REA,P=4081.00,Q=2,T=130622,N=40111789,E=372036|S=MMS,P=1269.00,Q=2,T=130622,N=30083125,E=372037|S=DMP,P=1488.00,Q=2,T=130622,N=40111790,E=372038|S=WPL,P=3733.00,Q=24,T=130622,N=30083126,E=372039|S=NWS,P=1928.00,Q=1,T=130622,N=40111791,E=372040|S=WBC,P=3218.00,Q=118,T=130622,N=20091814,E=372041|S=WOR,P=1610.00,Q=9,T=130622,N=30083127,E=372042|S=NWS,P=1928.00,Q=3,T=130622,N=40111792,E=372043|S=SUN,P=1298.00,Q=50,T=130622,N=20091815,E=372044|S=WES,P=4264.00,Q=1,T=130622,N=30083128,E=372045|S=RIO,P=6515.00,Q=1,T=130622,N=10085311,E=372046|S=SUL,P=1360.00,Q=6,T=130622,N=40111793,E=372047|S=WES,P=4264.00,Q=43,T=130622,N=30083129,E=372048|S=QBE,P=1566.00,Q=46,T=130622,N=20091816,E=372049|S=RIO,P=6515.00,Q=16,T=130622,N=10085312,E=372050|S=WOW,P=3347.00,Q=48,T=130622,N=40111794,E=372051|S=ORI,P=2302.00,Q=13,T=130622,N
Use tn.read_some() (blocking) instead of tn.read_very_eager() : it will wait for data to be available before returning

socket.recv() in python truncating output on newline?

Bottom-Line
The script does not print everything from socket.recv() to the linux terminal.
If I do the same thing in the interpreter, it prints all the data to the terminal.
(let's say i use local gateway for ip and 23 for port, so telnetting into my router)
import socket
q = socket.socket()
q.connect(ip, port)
data = q.recv(1024)
print data
Output in interpreter is four lines:
(some alt-code gibberish or whatever on the first line)
RT v24-sp2 std (c) 2012 NewMedia-NET GmbH
Release: 03/21/12 (SVN revision: 18795)
DD-WRT login:
Output from script:
(just the alt-code gibberish from the first line)
Any advice regarding why this is happening and how to correct it would be greatly appreciated.
Thanks,
Andrew
When running the commands slowly one at a time, your router has time to send everything it's planning to send before you have a chance to invoke q.recv(1024).
When you run it from a script, the commands execute in quick succession. When the script executes q.recv(1024), the router has only managed to send some data, not all of it.
Since you do not use a loop to go back and try reading more data, that's the end, you will not receive (or print) any more data.
(By the way, what in the world is "alt-code gibberish"? What you should be getting here is some binary data that's part of the telnet protocol negotiation.)

Categories

Resources