Socket send() method throws TypeError: a bytes-like object is required, not 'str'

python3 socket编程,发送data数据,会遇到需要bytes类型,而不是str字符串的错误

例如:

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname("www.google.com")
mysock.connect(host, 80)
message = "GET / HTTP/1.1\r\n\r\n"
mysock.sendall(message)
data=mysock.recv(1000)
mysock.close()

解决办法:

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname("www.google.com")
mysock.connect((host, 80))
message = "GET / HTTP/1.1\r\n\r\n"
mysock.sendall(message.encode())
data=mysock.recv(1000)
mysock.close()

只需要在string类型的data发送之前encode一下即可,接收也是一样

before sending message through socket encode it.

sc.send(message.encode())

after receiving decode it:

message.decode()

相关知识:

Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。Python 3不会以任意隐式的方式混用str和bytes,你不能拼接字符串和字节流,也无法在字节流里搜索字符串(反之亦然),也不能将字符串传入参数为字节流的函数(反之亦然)。

参考链接:https://www.cnblogs.com/chownjy/p/6625299.html

Socket send() method throws TypeError: a bytes-like object is required, not 'str'

原文地址:https://www.cnblogs.com/lzhd24/p/9575589.html

时间: 2024-08-07 15:37:31

Socket send() method throws TypeError: a bytes-like object is required, not 'str'的相关文章

【爬坑】Python 3.6 在 Socket 编程时出现类型错误 TypeError: a bytes-like object is required, not 'str'

1. 问题描述 Python 3.6 在 Socket 编程时出现错误如下 Traceback (most recent call last): File "F:/share/IdeaProjects/test/mypython/test/test10_tcpclient.py", line 17, in <module> sock.send(str) TypeError: a bytes-like object is required, not 'str' Process

Python Socket TypeError: a bytes-like object is required, not &#39;str&#39; 错误提示

在学习socket编程时,遇到代码返回如下错误: TypeError: a bytes-like object is required, not 'str' 发现这里python3.5和Python2.7在套接字返回值解码上有区别. 首先可以明确,在python3中默认的编码方式是unicode.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节),因此 utf-16就是现在最常用的unicode版本. 不过考虑到utf8省空间,在文件里存的

python问题:TypeError: a bytes-like object is required, not &#39;str&#39;

源程序: import socket target_host = "www.baidu.com" # 127.0.0.1 target_port = 80 # 建立一个socket对象 client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 创建TCP连接 # 连接客户端 client.connect((target_host,target_port)) client.send("GET / HTTP/1.1\r

python 3.5: TypeError: a bytes-like object is required, not &#39;str&#39;

出现该错误往往是通过open()函数打开文本文件时,使用了'rb'属性,如:fileHandle=open(filename,'rb'),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了str()函数,就会出现该错误,该错误不会再python2中出现. 具体解决方法有以下两种: 第一种,在open()函数中使用'r'属性,即文本方式读取,而不是'rb',以二进制文件方式读取,可以直接解决问题. 第二种,在open()函数中使用'rb',可以在使用之前进行转换,有以下实例,来自:htt

Python 3.5.2 TypeError: a bytes-like object is required, not &#39;str’问题解决方案

运行环境Mac  Python 3.5.2 Q: http_response = """\ HTTP/1.1 200 OK Hello, World! """ client_connection.sendall(http_response) TypeError: a bytes-like object is required, not 'str' 类型错误,需要的是一个byte类型,不是str类型 A: http_response = "

TypeError: a bytes-like object is required, not &#39;str&#39;

python bytes和str两种类型转换的函数encode(),decode() str通过encode()方法可以编码为指定的bytes 反过来,如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes.要把bytes变为str,就需要用decode()方法 例如: date="12345"  #str类型 date.encode()  #转换为bytes类型 TypeError: a bytes-like object is required, not 'str' 原文

解决python mysql插入数据时报错:TypeError: %d format: a number is required, not str

今天在使用python爬取数据并将其写入mysql数据库时,使用该如下语句: cursor.execute( "insert into comments_p_spider(owner_id,from_name,content,create_time,score,comment_level) values(%d,%s,%s,%s,%f,%s)", (p_id,str(username), str(contentStr), str(create_time),float(score), st

nginx报错 client intended to send too large body: 1331696 bytes

1,nginx后台error日志报错 2016/02/05 16:23:56 [error] 12024#0: *441106971 connect() failed (111: Connection refused) while connecting to upstream, client: 113.214.1.10, server: localhost, request: "GET /h5teb/ugcH5/index.htm?source=android&mall=8&TG

Socket send函数和recv函数详解

Socket send函数和recv函数详解 1.send 函数 int send( SOCKET s, const char FAR *buf, int len, int flags );  不论是客户还是服务器应用程序都用send函数来向TCP连接的另一端发送数据.客户程序一般用send函数向服务器发送请求,而服务器则通常用send函数来向客户程序发送应答. 该函数的第一个参数指定发送端套接字描述符: 第二个参数指明一个存放应用程序要发送数据的缓冲区: 第三个参数指明实际要发送的数据的字节数