5

I am trying to open a gif file, and send its bytes properly to a webbrowser but it throws the exception 'cannot convert 'bytes' object to str implicitly' and frankly I am baffled, because I already converted it to a string.

files=open("WebContent/"+fileName,"rb")
#size=os.path.getsize("WebContent/"+fileName)
text=str(files.read())
text=text.encode('UTF-8')
defaultResponseCode="HTTP/1.1 200 OK\r\nContent-Type: image/gif\r\nContent-Transfer-Encoding: binary\r\nContent-Length: 29696\r\n\r\n"+text

Thanks in advance!

3
  • 1
    text=text.encode('UTF-8') Now it's no longer a string... Commented Jan 12, 2014 at 14:48
  • Ohhhh so i should just do 'text.encode('UTF-8')'? Commented Jan 12, 2014 at 14:53
  • 1
    Why convert to string? You don't need that. Your Content-Transfer-Encoding header claims to be "binary", hence no encoding has been applied on the data. Send the image just as you read it from the file. Commented Jan 12, 2014 at 16:01

3 Answers 3

1

Here you are trying to convert bytes (file opened with 'rb' mode) to string:

text = str(files.read())

change above line to this:

text = files.read().decode(encoding='change_to_source_file_encoding')

then you can convert unicode string to utf-8 byte string with:

text = text.encode('UTF-8')

And if source encoding is utf-8 you can just pass byte string from files.read() to your result string without senseless decode/encode steps (from utf-8 bytes to string and again to utf-8 bytes)...

update: try with requests

url = 'url'
files = {'file': open("WebContent/"+fileName, 'rb')}
response = requests.post(url, files=files)

update 2:

According to content-type in response header (... Content-Type: image/gif ...) you have data with needed format just after files.read() whithout any encoding/decoding!

Sign up to request clarification or add additional context in comments.

6 Comments

@user1564622 try now, you are using python3?
I'm confused -- you should either read this as a string and use it as a string or read it as bytes and use it as bytes. Why both?
@user1564622 source file in utf-8 encoding?, try to change encoding or decoding with errors='ignore': text = files.read().decode(encoding='utf8', 'ignore')
No I mean the methodology doesn't make sense. You've having him read a file as bytes, then decode to string based on utf-8 encoding, then re-encoding that string to bytes, then sending out. @user1564622, skip the str(files.read()) call if you only need to sent it to your browser as bytes, and just send files.read(). If you need the plaintext string, save it to a different variable instead using files.read.decode(encoding='utf8') as ndpu suggested.
OK, bottom line, can someone show me read a file and send it correctly in http
|
0

For those primarily interested in Popen rather than psql, here's a debugged working python3 script, based the original Popen question, and incorporating provided suggestions.

#!/usr/bin/env python3

# print a list of postgresql databases via psql
import subprocess

username = "postgres"
dbname = "postgres"

p = subprocess.Popen(
  args = ['psql', '-qAt', '-U', username, '-d', dbname],
# args = ['psql', '-qAt', '-d', dbname], # with adequate user permissions
  stdin = subprocess.PIPE,
  stdout = subprocess.PIPE
)
sql = b"SELECT datname FROM pg_database;"

dbs = p.communicate(b"SELECT datname FROM pg_database;")[0].split(b'\n')

for db in dbs:
  print("%s" % db.decode("utf-8"))

p.wait() 

Comments

0

In python 3+ I was getting similar problem, and I removed text.encode("utf-8") and used text.strip().

text = text.strip()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.