Reputation: 13
I am new to Python. I am doing a course in Python 2.7, but at the same time, I want to be able to do everything in Python 3.
Code in Python 2.7:
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')
while True:
data = mysock.recv(512)
if ( len(data) < 1 ) :
break
print data
mysock.close()
Yields properly formatted data, like so:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Code in Python 3:
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send(('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n').encode())
while True:
data = mysock.recv(512)
if ( len(data) < 1 ) :
break
print(data);
mysock.close()
And it yields:
b'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset="utf-8"\r\nContent-Length: 2788\r\nConnection: Close\r\n\r\n<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n
As you can see, it adds a "b" character, and ignores \r \n. The formatting is way off. Where did the 'b' come from? How can I make python format it properly? I have tried converting it to a string, prior to printing but it didn't help.
Upvotes: 1
Views: 1917
Reputation: 160427
It has a b''
because what is returned by mysock.recv
is of type bytes
. You should decode your byte string to a unicode one with decode
:
print(data.decode('utf-8'))
Remember, Python 2 and 3 differ regarding strings as specified in PEP 3137
. Python 3 offers a clear separation between text and binary data, Python 2 doesn't.
The issue here is that when print
receives your bytes object, it'll call str
on it which will simply build a string out of it as it knows best; i.e escape backslashes and retain the rest:
>>> str(b"hello\nworld")
"b'hello\\nworld'"
Then print
will just take that and print it out.
Upvotes: 4