I have a python configuration script, the problem occurs when I import readline library and enter the console of the device. I get the input from the user this way:
val = input(display_str).strip()
This piece of code works perfectly but in the console of the device, long strings are not formatted properly and looks like it puts a limitation how long the string is and prints then on top of each-other. This makes the string unreadable and looks like this:
D/E,GATEWAY or N to delete : in format A:B:C:D:E:F:G:H/K,GATEWAY A:A:B:B:C:C:D:D
when in reality it needs to be formatted like this:
Enter eth1 IPv6 static routes in format A:B:C:D:E:F:G:H/K,GATEWAY A:A:B:B:C:C:D:D/E,GATEWAY or N to delete :
The formatting works perfectly when I ssh or use monitor but when I connect with console it mixes. When I remove the library everything works smoothly.
I found another solution to this problem which is using:
sys.stdout.write(display_str)
val = input().strip()
However here another problem occurs, the user can move left to the beginning of the string and delete the display_str and is not suitable. Other than then it works normally in every mode. Is there a solution for the input function or for the sys.stdout
Unfortunately still using Python 2.7 and moving to Python 3 currently is not in the roadmap.
I am making a REST call to an external application to retrieve data which I need to then process, push into a CSV file and post that across another different REST interface.
The characters causing me problems I have receive from the first rest call are
u'\xe7\x83\xad\xe7\x82\xb9\xe8\xae\xa8\xe8\xae\xba'
which when printed are
>>> print u'\xe7\x83\xad\xe7\x82\xb9\xe8\xae\xa8\xe8\xae\xba'
çƒç‚¹è®¨è®º
What this should actually be is 热点讨论
Now this data is taken and then processed without change but is written into the csv file as 'çƒç‚¹è®¨è®º' and then posted to the second REST interface
The actual characters I should be receiving from the CSV file should be
热点讨论
which in unicode should look like u'\u70ed\u70b9\u8ba8\u8bba'
>>> print u'\u70ed\u70b9\u8ba8\u8bba'
热点讨论
How do I either:
transform the initially received unicode u'\xe7\x83\xad\xe7\x82\xb9\xe8\xae\xa8\xe8\xae\xba' into u'\u70ed\u70b9\u8ba8\u8bba' before pushing into the CSV file and posting to the second rest api
Cause u'\xe7\x83\xad\xe7\x82\xb9\xe8\xae\xa8\xe8\xae\xba' to print 热点讨论 correctly once on the receive side of the second REST api?
Edit: for the first REST call.
The call is being made using requests.get()
This is the response data:
[
{"flags":1074200578,"results":{"id1":22212362104,"id2":22212362125,"fields":[{"id1":0,"id2":0,"count":0,"format":128,"type":"ip.src","flags":0,"group":0,"value":"172.18.16.74"},{"id1":0,"id2":0,"count":0,"format":65,"type":"filename","flags":0,"group":0,"value":"WinVNC6"},{"id1":0,"id2":0,"count":0,"format":65,"type":"checksum","flags":0,"group":0,"value":"5b52bc196bfc207d43eedfe585df96fcfabbdead087ff79fcdcdd4d08c7806db"},{"id1":22212362104,"id2":22212362104,"count":0,"format":32,"type":"time","flags":0,"group":0,"value":1618530308},{"id1":22212362106,"id2":22212362106,"count":0,"format":128,"type":"device.ip","flags":0,"group":0,"value":"172.18.21.197"},{"id1":22212362108,"id2":22212362108,"count":0,"format":65,"type":"device.type","flags":0,"group":0,"value":"symantecav"},{"id1":22212362111,"id2":22212362111,"count":0,"format":65,"type":"host.src","flags":0,"group":0,"value":"\u00E7\u0083\u00AD\u00E7\u0082\u00B9\u00E8\u00AE\u00A8\u00E8\u00AE\u00BA"},{"id1":22212362114,"id2":22212362114,"count":0,"format":65,"type":"checksum","flags":0,"group":0,"value":"5b52bc196bfc207d43eedfe585df96fcfabbdead087ff79fcdcdd4d08c7806db"},{"id1":22212362117,"id2":22212362117,"count":0,"format":65,"type":"filename","flags":0,"group":0,"value":"WinVNC6"},{"id1":22212362119,"id2":22212362119,"count":0,"format":65,"type":"action","flags":0,"group":0,"value":"Left alone"},{"id1":22212362124,"id2":22212362124,"count":0,"format":65,"type":"user.dst","flags":0,"group":0,"value":"Adam_Joe"},{"id1":22212362125,"id2":22212362125,"count":0,"format":128,"type":"ip.src","flags":0,"group":0,"value":"172.18.16.74"},{"id1":22212362104,"id2":22212362104,"count":1,"format":8,"type":"time","flags":2,"group":0,"value":"1"}]}},
{"flags":1074200577,"results":{"id1":22212362126,"id2":22212362136,"fields":[]}}
]
The piece in this causing the issues is "\u00E7\u0083\u00AD\u00E7\u0082\u00B9\u00E8\u00AE\u00A8\u00E8\u00AE\u00BA"
To explain you my Problem, my Client sends a username and password...
The Client format it like this
file = salt + username + "splitme" + salt + password
encrypt file via tls (when connecting to server)
Send to Server
server decrypts the file and trys to split at "splitme"
CODE:
data = data.split("splitme", 1)
username_salted = data[0]
password_salted = data[1]
And then Python stops the program and says
password_salted = data[1]
IndexError: list index out of range
I don't know whats the problem ...
Because data is a list of 1 element, since the first element access did not raise an error.
Did you decode data? Did it succeed?
data probably does not contain "splitme", in which case you get a list with the whole content you tried to split as the only element.
Have you verified the contents of data? Do you have access to the source code? Can you run it? In that case, you can either change the code for some extra logging, run a debugger such as ipdb3 or use the trace module to see what gets executed.
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.
I have to write 7231 bytes into a file using python script. In a client-server program, my python script act like client and it received 7231 bytes from server. If I check in TCP-Dump, its shows complete data. But when I try to write into a file; I am missing the content.
My script:
def SendOnce(self, req='/gpsData=1',method="GET"):
conn = httplib.HTTPConnection(self.proxy)
self.Logresponse("\nConnection Open\n<br />")
conn.request(method,req)
Log="\nRequest Send: %s\n<br \>\n" %req
self.Logresponse(Log)
response = conn.getresponse()
Log = "\nResponse Code: %s\n<br \>\n" %response.status
self.Logresponse(Log)
Log = "\nSarav -- Get Header: %s \n version= %s <br \>\n" %(response.msg,response.version)
self.Logresponse(Log)
if (response.status==200):
Log = response.read()
self.Logresponse(Log)
conn.close()
self.Logresponse("\nConnection Close\n<br \>")
return response
this "self.Logresponse(Log)" is writing into file. If i receive 1023 bytes, its writing full content into that. Please help me out how to write complete data.
Note: I am writing Hexa Format data.
First of all, 7231 bytes is not exactly huge...
With the limited info you gave, I would guess that you might have forgotten to take the OS's write buffer into account. You probably try to read the file before all the content was written to it.
Python generally uses the system's standard buffer (you can change that). You can decrease that buffer, or force a flush yourself.
I'm just guessing, it might be that the .read() function doesn't return all data in one chunk; can you try to modify the inner part like this:
if (response.status==200):
while 1:
Log = response.read()
if not Log:
break
self.Logresponse(Log)