python基础知识的重新认识
昨天模拟书本上client和server交互的例子,代码明明是按照书上写的,可是就是出现了错误,像下面这样:
# tcpserver
from socket import *
from time import ctime HOST =''
PORT = 6555
BUFSIZ = 2048
ADDR = (HOST,PORT) tcpservsock = socket(AF_INET,SOCK_STREAM)
tcpservsock.bind(ADDR) tcpservsock.listen(5) while True: print('waiting for connection...')
tcpClisock,addr = tcpservsock.accept()
print('...connected from:',addr) while True:
data = tcpClisock.recv(BUFSIZ)
if not data:
break
print(data.decode('utf8'))
tcpClisock.send('[%s]%s'%(bytes(ctime(),'utf8'),data)) # 这里弹出错误
tcpClisock.close() tcpservsock.close()
#tcpClient
from socket import *
HOST = 'localhost'
PORT = 6555
BUFSIZ = 2048
ADDR = (HOST,PORT) tcpClisock = socket(AF_INET,SOCK_STREAM)
tcpClisock.connect(ADDR)
while True:
data = input('> ')
if not data: break
tcpClisock.send(data.encode())
data = tcpClisock.recv(BUFSIZ)
if not data:
break
print(data.decode('utf-8')) tcpClisock.close()
然后错误就出现了:
waiting for connection...
...connected from: ('127.0.0.1', 3786)
hi
Traceback (most recent call last):
File "C:\Users\yfg\Desktop\server.py", line 26, in <module>
tcpClisock.send('[%s]%s'%(bytes(ctime(),'utf8'),data))
TypeError: a bytes-like object is required, not 'str'
在苦思冥想之后,我发现:在server.py line26中 ,我将二进制数据放在了 字符串格式化符后面,这是不对的,所以只要在原来 '[%s]%s'%(bytes(ctime(),'utf8'),data) 前面 加上 b ,就像这样 b'[%s]%s'%(bytes(ctime(),'utf8'),data) ,然后 跑起来 :在服务器这边得到这个 :
,在客户端这边得到这个:
问题就解决了 。 总结一下就是:字符串格式化后跟字符串,二进制格式化要在字符串格式前加字母b。
python基础知识的重新认识的更多相关文章
- Python开发【第二篇】:Python基础知识
Python基础知识 一.初识基本数据类型 类型: int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位 ...
- python基础知识(二)
以下内容,作为python基础知识的补充,主要涉及基础数据类型的创建及特性,以及新数据类型Bytes类型的引入介绍
- python 基础知识(一)
python 基础知识(一) 一.python发展介绍 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本 ...
- python基础知识讲解——@classmethod和@staticmethod的作用
python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...
- python 爬虫与数据可视化--python基础知识
摘要:偶然机会接触到python语音,感觉语法简单.功能强大,刚好朋友分享了一个网课<python 爬虫与数据可视化>,于是在工作与闲暇时间学习起来,并做如下课程笔记整理,整体大概分为4个 ...
- python基础知识小结-运维笔记
接触python已有一段时间了,下面针对python基础知识的使用做一完整梳理:1)避免‘\n’等特殊字符的两种方式: a)利用转义字符‘\’ b)利用原始字符‘r’ print r'c:\now' ...
- Python基础知识(五)
# -*- coding: utf-8 -*-# @Time : 2018-12-25 19:31# @Author : 三斤春药# @Email : zhou_wanchun@qq.com# @Fi ...
- Python基础知识(Basic knowledge)
Python基础知识(Basic knowledge) 1.认识Python&基础环境搭建 2.Python基础(上) 3.Python基础(中) 4.Python基础(下) 5.Python ...
- Python 基础知识(一)
1.Python简介 1.1.Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时 ...
随机推荐
- MySQL出现错误1030-Got error 28 from storage engine
磁盘空间不足引起的!1030-Got error 28 from storage engine df -h 清理空间
- TOJ-3474 The Big Dance(递归二分)
链接:https://ac.nowcoder.com/acm/contest/1077/L 题目描述 Bessie and the herd, N (1 <= N <= 2,200) co ...
- pip 通过pqi切换源到国内镜像
pip install pqipqi lspqi use aliyun # pqi use tuna 清华
- 查询Redis缓存
package me.zhengjie.monitor.rest; import me.zhengjie.common.aop.log.Log; import me.zhengjie.monitor. ...
- the least-squares criterion|Sxx|Sxy|Syy|Regression Equation|Outliers|Influential Observations|curvilinear regression|linear regression
4.2 The Regression Equation Because we could draw many different lines through the cluster of data p ...
- Linux shell Script初识
shell secript: 执行方式的差异: ./ sh执行都是在创建一个子程序来执行,只会继承环境变量, 其中的变量如果export声明,子程序的子程序会继承,不会升级为环境变量 source 的 ...
- youths |government|some
N-COUNT (新闻用语,尤指惹麻烦的)青年,小伙子Journalists often refer to young men as youths, especially when they are ...
- vue-cli3初始化项目
1 npm install -g @vue/cli 创建配置 创建 1 vue create vue-app 选择配置 1234 ? Please pick a preset: (Use arrow ...
- 同步linux系统时间
Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...
- css - 原生变量及使用函数 var()
零.序言 前两天在逛 blog 的时候看见一些内联样式新奇的写法时很纳闷,虽然说不上多么熟练,但是从来没见过 --color: brown 这样的写法,百度一番之后仍然没啥头绪,今天偶然看到一篇文章 ...