I am making a server that asks for you IP address and I want to make an option that says (leave blank for __your ip__) but I'm having trouble with it. I tried doing this
import subprocess
output = subprocess.check_output("ipconfig getifaddr en0", shell=True)
but it returns
b'***.***.***.***\n' is there a way to remove the '', b and \n or another way to get an IP that doesn't return 127.0.0.1?
What you're seeing is the bytestring output and the following newline.
You'll want
output = subprocess.check_output("ipconfig getifaddr en0", shell=True).decode().strip()
to first decode the bytes as UTF-8, then strip whitespace from the start and the end.
Related
I am attempting to read a string from serial line using the code below, python keeps attaching the b' prefix and newline or return suffixes despite my telling it to convert to regular code and strip those out. Also, even if I send the text for 'FORWARD' to the device, it will not recognize the response.
Why wont python convert my text to regular format, and how do I get it to recognize my input.
#!/usr/bin/env python
import serial
ser = serial.Serial(port='/dev/ttyAMA0',baudrate = 9600,timeout=1)
while 1:
x=ser.readString()
x = x.decode()
x = x.strip()
print(x)
if x.find('FORWARD') >= 0:
print("FORWARD")
I expect it to show my serial input without the b' prefix or any \r\n suffixes, just the text I sent. I also expect it to recognize that the word "FORWARD" was in my input when I send that over the serial line. It dont do that, it shows b'FORWARD\r\n' and dont recognize that FORWARD is in the text
Hello I think decode('UTF-8') might help. I am working with binary data as well and you should specify which form you want it to be decoded to.
b'test'.decode('utf-8') == 'test' -> True
If the problem roots in that your string is binary which contains binary string, such as b'b"test"' then you can solve it with:
b'b"test"'.decode('utf-8').strip('b"') == 'test' -> True
I am using Python sub process to execute command as shown below:
process = subprocess.Popen(['debug', 'file.tgz'],
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
while True:
output = process.stdout.readline()
print(str(output.strip()).encode())
return_code = process.poll()
if return_code is not None:
break
What i am getting out put is shown below:
b"b'Registers:'"
And this is what i am expecting.
Registers:
I am using encode but still seeing same out put. If i run the same process on command line i am getting the same desired out put.
How can i remove these special characters?
Skip the str(); that'll get rid of the inner b'...'
You want to .decode rather than .encode, because you want to turn a byte-stream (which came from the subprocess) into a string (for printing). You'll need to know which encoding it's using, and (if applicable) what to do with malformed data.
(optional) Strip the whitespace after decoding, not before, to also strip non-ASCII whitespace characters (if any).
print(output.decode('utf8', errors='strict').strip())
I've just started to write a monitoring tool in Python 3, and I'm wondering if I can get a 'clear' number output through ssh. I've written this script:
import os
import paramiko
command = 'w|grep \"load average\"|grep -v grep|awk {\'print ($10+$11+$12)/3*100\'};'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy())
ssh.connect('10.123.222.233', username='xxx', password='xxx')
stdin, stdout, stderr = ssh.exec_command(command)
print (stdout.readlines())
ssh.close()
It works fine, except the output is:
['22.3333\n']
How can I get rid of the [' and \n'] and just get the clear number value?
How can I get the result as I see it in PuTTy?
.readlines() returns a list of separate lines. In your case there is just one line, you can just extract it by indexing the list, then strip of the whitespace at the end:
firstline = stdout.readlines()[0].rstrip()
This is still a string, however. If you expected numbers, you'd have to convert the string to a float(). Since your command line will only ever return one line, you may as well just use .read() and convert that straight up (no need to strip, as float() is tolerant of trailing and leading whitespace):
result = float(stdout.read())
Instead of print(stdout.readlines()) you should iterate over each element in the list that is returned by stdout.readlines() and strip it, possibly converting it to float as well (depends what you are planning to do with this data later).
You can use list comprehension for this:
print([float(line.strip()) for line in stdout.readlines()])
Note that strip will remove whitespaces and new-line chars from both the start and end of the string. If you only want to remove the trailing whitespace/new-line char then you can use rstrip, but note that the conversion to float may then fail.
I'm dealing 2 scripts in python. the first one needs to send a value or an argument to the second script. Now the problem is whenever I send the value to the 2nd script, the 2nd script couldn't get all the arguments that I sent. the value that Im sending is a URL and it contains an ampersand. I noticed that it kept on cutting the value to the first appearance of &.
lets sat for example, I need to pass this :
http://www.google.com/jfljflfjej&12345
the 2nd script will receive only this :
http://www.google.com/jfljflfjej
what do I need to do to be able to catch the correct value? And what other characters that have the same issue as this?
You need to put quotes around the whole value, including the ampersand, as it is a special shell character. Alternatively, you can escape just the ampersand by putting a backslash in front of it:
http://www.google.com/jfljflfjej\&12345
The ampersand signals to the shell you want to put the command up to that point in background mode.
Any of the following characters are special in a shell:
\ ' " ` < > | ; <Space> <Tab> <Newline> ( ) [ ] ? # $ ^ & * =
These need to be escaped in the same way; use a backslash or put quotes around the value.
Maybe it's a bit late to answer you but I had the same problem and I found a simple solution to that.
You can use the function replace() to replace every ampersand with their code in HTML '%26' like this.
url = http://www.google.com/jfljflfjej&12345
print url.replace('&', '%26')
And the result:
http://www.google.com/jfljflfjej%2612345
It's a problem of coding.
In Python 3:
import urllib.parse
url = urllib.parse.quote("http://www.google.com/jfljflfjej&12345")
I have this code in Python
inputted = input("Enter in something: ")
print("Input is {0}, including the return".format(inputted))
that outputs
Enter in something: something
Input is something
, including the return
I am not sure what is happening; if I use variables that don't depend on user input, I do not get the newline after formatting with the variable. I suspect Python might be taking in the newline as input when I hit return.
How can I make it so that the input does not include any newlines so that I may compare it to other strings/characters? (e.g. something == 'a')
You are correct - a newline is included in inputted. To remove it, you can just call strip("\r\n") to remove the newline from the end:
print("Input is {0}, including the return".format(inputted.strip("\r\n")))
This won't cause any issues if inputted does not have a newline at the end, but will remove any that are there, so you can use this whether inputted is user input or not.
If you don't want any newlines in the text at all, you can use inputted.replace("\r\n", "") to remove all newlines.
Your problem is actually Eclipse. Assuming that you use PyDev, I was able to reproduce the problem. When entering something in the Eclipse console, the problem occurs as described in your question. But when directly executing the very same script with the Python 3.1.1 interpreter, inputted does not include a newline character.
I investigated the Python source code and found out input() uses GNU readline if stdin is interactive (i.e. a TTY or prompt, however you want to call it), but falls back to the .readline() method of the stdin object if necessary. Then, if the result of readline ends with \n, that character is removed. Note: No CR-LF or LF-CR handling here (in the fallback case)!
So I wrote this little script to see what actually happens:
import sys
from io import StringIO
for stdin in [sys.stdin, StringIO("test\r\ntest\r\n")]:
sys.stdin = stdin
print("readline returns this: " + repr(sys.stdin.readline()))
inputted = input("Enter in something: ")
print("inputted: " + repr(inputted))
print("inputted is printed like this: --> {0} <--".format(inputted))
It first executes the code with the normal stdin (console or Eclipse console) and then with a prepared stdin containing the text test\r\ntest\r\n.
Try and run the script in Eclipse - you must enter a string twice. The conclusion: Pressing Enter in the Eclipse console will produce CR-LF ("\r\n"). Printing "\r" in the Eclipse console will jump to the next line.
On the other side, running it in the Windows console will produce the expected output: input() returns a string without a newline at the end because (I guess) GNU readline is used. With the prepared stdin StringIO("test\r\n"), the input() result is "test\r" as in Eclipse (although not printed as newline).
Hope this all makes sense... but what I still don't know is if that is expected behavior of Eclipse.
If you only want to stript the last line endings, you could use rstrip.
inputted.rstrip ("\r\n")
inputted = inputted.strip()
Edit: As noted, this will kill all whitespace at the start and end. A way to get rid of only the trailing newline is:
import re
inputted = re.sub("[\n\r]+$", "", inputted)