I have been trying to convert a list of string elements to bytes so that I can send it to the server.
Below is the snippet for my code:-
ls_queue=list(q.queue)
print("Queue converted to list elements:::",ls_queue)
encoded_list=[x.encode('utf-8') for x in ls_queue]
print("Encoded list:::",encoded_list)
s.send(encoded_list)
The output I get is:
Encoded list::: [b'madhav']
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in
__call__
return self.func(*args)
File "Practice_Client_Server.py", line 149, in Word_Append_Window
s.send(encoded_list)
TypeError: a bytes-like object is required, not 'list'
I can see that it is getting converted to bytes but it still gives the error while trying to encode and send. Can someone take a look as to what I am doing wrong here?
Thank you
You are sending a list object when send is expecting a bytes one, that happened when u converted the elements of the list into bytes but not the list container. What u can do is serialize it as a JSON string and then convert it to bytes, for example:
import json
l = ['foo', 'bar']
l_str = json.dumps(l)
l_bytes = l_str.encode('utf-8')
send(l_bytes)
Then u can read it on your server doing the opposite:
reconstructed_l = json.loads(l_bytes.decode('utf-8'))
Related
I'm having trouble wrapping my brain around this error. It's really basic, but it seems to say the opposite of what is true.
>>> x=b'hi'
>>> urllib.parse.unquote(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/parse.py", line 609, in unquote
if '%' not in string:
TypeError: a bytes-like object is required, not 'str'
I've passed the code a bytes object, I would hope that's bytes-like enough. urllib.parse.unquote() seems to only work with a str object.
So why would it generate an error saying it needs a bytes-like object?
I suggest to use urllib.parse.unquote_to_bytes, which encodes its string parameter to bytes if it receives a str object.
urllib.parse.unquote_to_bytes(x)
I have been working on a program that allows you to enter any text you want and it will return the hashed result in sha256. However i am receiving an error on line 4 The whole error message:
Traceback (most recent call last):
File "main.py", line 4, in <module>
hash_object = hashlib.sha256(password_sign_up)
TypeError: Unicode-objects must be encoded before hashing
The code:
import hashlib
password_sign_up = input("Enter your desired password: ")
hash_object = hashlib.sha256(password_sign_up)
hex_dig = hash_object.hexdigest()
print(hex_dig)
You have to use .encode('utf-8') for your password.
In python 2x, the default encoding for any string is unicode. But in 3x, you will have to encode it to your choice of encoding. e.g. utf-32, utf-8 etc.
Try this: hash_object = hashlib.sha256(password_sign_up.encode('utf-8'))
You're taking the result of the input() function (which returns a str object) and putting it directly into sha256(). The sha256() function requires its parameter to be a bytes object.
You can convert a string to a bytes with:
myNewBytesObject = password_sign_up.encode('utf-8')
I want to decompress the following gz compressed string using python3.6:
H4sIAAAAAAAA//NIzcnJVwjPL8pJAQBWsRdKCwAAAA==
The decompressed string is "Hello World"
I was able to decompress it using online tool - http://www.txtwizard.net/compression but I couldn't find a proper way to do it in python.
I tried zlib and gzip, but they require bytes not str. I also tried converting it using io.Bytes() but of no use. My Code is:
import gzip
import io
class SearchEvents:
def decompressPayload():
payload = "H4sIAAAAAAAA//NIzcnJVwjPL8pJAQBWsRdKCwAAAA=="
payload_bytes = io.BytesIO(payload)
print(gzip.decompress(payload_bytes))
SearchEvents.decompressPayload()
I am expecting "Hello World" as output. But I am getting the following error:
Traceback (most recent call last):
File "SearchEvents.py", line 13, in <module>
SearchEvents.decompressPayload()
File "SearchEvents.py", line 10, in decompressPayload
payload_bytes = io.BytesIO(payload)
TypeError: a bytes-like object is required, not 'str'
Is there any way to achieve what I want?
I want to decompress the following gz compressed string using python3.6:
...==
That's not a gzip-compressed string. At least, not until you Base64-decode it first.
>>> gzip.decompress(base64.b64decode('H4sIAAAAAAAA//NIzcnJVwjPL8pJAQBWsRdKCwAAAA=='))
b'Hello World'
For stuff that needs bytes, give it bytes. Add the b prefix to make a bytes literal, e.g.:
gzip.decompress(b"H4sIAAAAAAAA//NIzcnJVwjPL8pJAQBWsRdKCwAAAA==")
This doesn't work, because that's not valid compressed data. It looks like it's base64 encoded though, so by combining it with binascii, you get:
import binascii
import gzip
gzip.decompress(binascii.a2b_base64(b"H4sIAAAAAAAA//NIzcnJVwjPL8pJAQBWsRdKCwAAAA=="))
Which produces b'Hello World'
I always get this error...
Traceback (most recent call last):
File "C:/Users/01/Desktop 3/Projects/univ/number.py", line 11, in <module>
print(line[5])
IndexError: string index out of range
I just want to read information from a txt file
readFile = open("utf.txt", encoding="utf-8").read()
for line in readFile:
print(line[5])
I set txt encoding to "utf-8", my IDE also has the same encoding set
One more thing to concider: the file is written in Russian
Your readFile variable is a string (because of .read() method). When you iterate over it you get one char (this is your line variable). Then you try to print sixth element of this one char. Of course you get IndexError.
I originally put this code through Python 2.7 but needed to move to Python 3.x because of work. I've been trying to figure out how to get this code to work in Python 3.2, with no luck.
import subprocess
cmd = subprocess.Popen('net use', shell=True, stdout=subprocess.PIPE)
for line in cmd.stdout:
if 'no' in line:
print (line)
I get this error
if 'no' in (line):
TypeError: Type str doesn't support the buffer API
Can anyone provide me with an answer as to why this is and/or some documentation to read?
Much appreciated.
Python 3 uses the bytes type in a lot places where the encoding is not clearly defined. The stdout of your subprocess is a file object working with bytes data. So, you cannot check if there is some string within a bytes object, e.g.:
>>> 'no' in b'some bytes string'
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
'no' in b'some bytes string'
TypeError: Type str doesn't support the buffer API
What you need to do instead is a test if the bytes string contains another bytes string:
>>> b'no' in b'some bytes string'
False
So, back to your problem, this should work:
if b'no' in line:
print(line)