Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can only concatenate str (not "bytes") to str

Tags:

import socket
import os

user_url = input("Enter url: ")

host_name = user_url.split("/")[2]
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((host_name, 80))
cmd = 'GET ' + user_url + ' HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
     print(data.decode(),end='\n')

mysock.close()

For some reason im gettin this error

Enter url: http://data.pr4e.org/romeo.txt

 7 mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 8 mysock.connect((host_name, 80))
 9 cmd = 'GET ' + user_url + ' HTTP/1.0\r\n\r\n'.encode()
 TypeError: can only concatenate str (not "bytes") to str

Any ideas what im doing wrong with it?Encoding and decoding seems right to me, and i've trasnfered it using \n before .encode(). This is for a class

like image 740
Pharah181 Avatar asked Mar 06 '19 22:03

Pharah181


People also ask

Can only concatenate str not set to STR?

The Python "TypeError: can only concatenate str (not "set") to str" occurs when we try to concatenate a string and a set. To solve the error, use a formatted string literal, or use the add() method to add an item to the set.

How do you fix TypeError can only concatenate str not function to str?

The Python "TypeError: can only concatenate str (not "int") to str" occurs when we try to concatenate a string and an integer. To solve the error, convert the int to a string, e.g. str(my_int) to concatenate the strings or convert the str to an int, e.g. int(my_str) to add the numbers.

Why can't Python concatenate str and int objects?

In Python, we cannot concatenate a string and an integer together. They have a different base and memory space as they are completely different data structures.


Video Answer


2 Answers

A str is an abstract sequence of Unicode code points; a bytes is a sequence of 8-bit numbers. Python 3 made the distinction between the two very clear and does not allow you to combine them implicitly. A str may have several valid encodings, and a bytes object may or may not be the encoding of a valid Unicode string. (Or, the bytes could be the encoding of multiple different str objects depending on the encoding used to create it.)

'GET ' and user_url are str objects, while ' HTTP/1.0\r\n\r\n'.encode() is a bytes object. You want to encode the entire concatenated string instead.

cmd = 'GET {} HTTP/1.0\r\n\r\n'.format(user_url).encode()

Or perhaps written to show the steps more clearly,

cmd = 'GET {} HTTP/1.0\r\n\r\n'.format(user_url)  # still a str
mysock.send(cmd.encode())  # send the encoding of the str
like image 91
chepner Avatar answered Oct 17 '22 18:10

chepner


The problem is that you're encoding before concatenating:

'GET ' + user_url + ' HTTP/1.0\r\n\r\n'.encode()

You have to concatenate first, and then encode the entire thing:

('GET ' + user_url + ' HTTP/1.0\r\n\r\n').encode()
like image 33
Aran-Fey Avatar answered Oct 17 '22 18:10

Aran-Fey