Python TypeError: a bytes-like object is required, not 'str' -


i using below script make request google. however, when execute it, error stating:

client.send("get / http/1.1\r\nhost: google.com\r\n\r\n") typeerror: bytes-like object required, not 'str'

on browsing similar issues in stackoverflow, client.sendto has been recommended use along encoding message. however, in case there no message passed.

the below script use:

import socket target_host = "www.google.com" target_port = 80  client = socket.socket(socket.af_inet, socket.sock_stream) client.connect((target_host,target_port)) client.send("get / http/1.1\r\nhost: google.com\r\n\r\n") response = client.recv(4096) print(response) 

any pointers on correcting issue appreciated.

you need choose encoding, example:

client.send("get / http/1.1\r\nhost: google.com\r\n\r\n".encode(encoding='utf-8')) 

see also: python socket send buffer vs. str , typeerror: 'str' not support buffer interface


Comments