python问题:TypeError: a bytes-like object is required, not 'str'
源程序:
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\nHost: baidu.com\r\n\r\n")
错误背景:程序想创建一个TCP连接,在发送数据的时候报错,表明send函数需要传byte类型值。
类型错误:TypeError: a bytes-like object is required, not 'str'
解决方法:
1、在数据前面加b,强制转换
client.send(b"GET / HTTP/1.1\r\nHost: baidu.com\r\n\r\n")
2、发送数据的时候进行编码
client.send(("GET / HTTP/1.1\r\nHost: baidu.com\r\n\r\n").encode())
这里我看了一篇前人MrYx的文章,说的很好 http://blog.csdn.net/yexiaohhjk/article/details/68066843
python问题:TypeError: a bytes-like object is required, not 'str'的更多相关文章
- python 报错:a bytes-like object is required, not 'str'
核心代码: def ipPools(numPage): headers = randomHeads() url = 'http://www.xicidaili.com/nn/' saveFsvFile ...
- 【爬坑】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/IdeaPro ...
- python3 TypeError: a bytes-like object is required, not 'str'
在学习<Python web开发学习实录>时, 例11-1: # !/usr/bin/env python # coding=utf-8 import socket sock = sock ...
- TCP客户端和服务器间传输数据遇到的TypeError: a bytes-like object is required, not 'str'问题
使用python实现python核心编程3第472页和474页的TCP时间戳服务器和客户端服务器间数据传输编程时遇到TypeError: a bytes-like object is required ...
- moviepy执行TextClip.search方法时报错TypeError: a bytes-like object is required, not str
☞ ░ 前往老猿Python博文目录 ░ 执行TextClip.search方法时,报错: >>> from moviepy.editor import * >>> ...
- linux下运行python3出现TypeError: a bytes-like object is required, not 'str'
目标:用python将中文存入csv,且中文正常显示. 环境:linux,python3 百度N久,方法都不行或是比较复杂. 以上代码用python3运行后,出现TypeError: a bytes- ...
- sbt package报错:a bytes-like object is required, not 'str'
Traceback (most recent call last): File , in <module> s.sendall(content) TypeError: a bytes-li ...
- python中MySQL模块TypeError: %d format: a number is required, not str异常解决
转载自:http://www.codeif.com/topic/896 python代码: attr_sql = "INSERT INTO `ym_attribute` (`attr_nam ...
- python 3.5: TypeError: a bytes-like object is required, not 'str'
出现该错误往往是通过open()函数打开文本文件时,使用了'rb'属性,如:fileHandle=open(filename,'rb'),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了s ...
随机推荐
- 开发问题及解决--java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android=" ...
- java基础学习2
http://www.runoob.com/java/java-modifier-types.html Java 修饰符 Java 增强 for 循环 Java5 引入了一种主要用于数组的增强型 ...
- VirtualBox安装linux
VBox相较于VMware要小巧,虚拟机该有的都有了.搭建记录下,学习... centos版本:CentOS-6.6-i386-bin-DVD1 VBox版本:6.0.4-128413-Win 之后可 ...
- 上这个资源网站,让你轻松无忧找mac软件资源
之前分享过好几篇关于mac软件相关的文章(想要看其他的mac软件专题文章,可以关注我,点击进入查看发表的文章),有网友表示,优质的软件推荐清单有了,想要下载和获取mac软件,买一个正版软件在APP s ...
- django-rest-framework-jwt
官网文档:http://getblimp.github.io/django-rest-framework-jwt/ GitHub:https://github.com/GetBlimp/django- ...
- c++后台开发路线
- 01-Unity深入浅出(一)
一. 温故而知新 在开始学习Unity框架之前,有必要温习一下 [依赖倒置原则]和[手写IOC], 因为我们框架代码的构建都是基于[依赖倒置原则]的,而Unity框架的核心思想就是IOC和DI,所以有 ...
- State management(状态管理)
State management https://en.wikipedia.org/wiki/State_management UI控件的状态管理, 例如按钮的灰化.只读.显示隐藏等. 特殊地一个控件 ...
- Content Security Policy介绍
Content Security Policy https://content-security-policy.com/ The new Content-Security-Policy HTTP re ...
- 【深入理解JVM】:Java类继承关系中的初始化顺序
尝试着仔细阅读thinking in java 看到一篇很好的文章http://blog.csdn.net/u011080472/article/details/51330114