重点回顾:
(重点)粘包 : 就是因为接收端不知道如何接收数据,造成接收数据的混乱的问题 只发生在tcp协议上. 因为tcp协议的特点是面向数据流形式的传输 粘包的发生主要是因为tcp协议有两个机制: 合包机制(nagle算法),拆包机制 subprocess 模块 有一个方法可以执行系统命令 Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) struct 模块 有一个方法可以将21.3E以内的数据,打包成4个长度的bytes r = struct.pack('i',num) struct.unpack('i',r) (重点)架构: C/S架构 B/S架构(优点:统一了应用的接口) 多台电脑通信 : 交换机+路由器 mac地址: 物理地址,全球唯一 身份证号 ip地址 : 虚拟地址,四位点分十进制 学号 (重点)如何判断两台主机是否在同一个局域网? ip地址 & 子网掩码 = 网段 (重点)arp协议: 通过目标ip地址,获取目标mac地址 端口:操作系统给予,通过端口号,可以确定某一个应用程序 (重点)ip+端口:唯一确定某一个主机上的某一个应用程序 回环地址:127.0.0.1 osi五层模型: 应用层 py文件,应用 传输层 tcp/udp协议 网络层 ip协议 数据链路层 arp协议,网卡 物理层 网线,集线器,光纤 (重点)tcp协议:安全,可靠,面向连接,面向数据流形式的传输 三次握手: (面试回答) 首先,必须先由客户端发起连接的请求 接下来,服务器接收到请求之后,回复给客户端两个标识,一个syn表示 服务器接收到请求,一个ack表示服务器在做准备工作,两个标识一起 回复给客户端 最后,客户端接收到服务器的回复,客户端准备连接的所有资源,开始进行连接 发送给服务器一个ack表示客户端的连接准备工作已经完成 (此时表示客户端和服务器可以相互连接了) 如果面试官问你,哪句代码体现了三次握手? 回答: 服务器端的accept,客户端connect 四次挥手: (面试回答) (1)首先由连接双方任意一方发起断开连接的请求,发起方发送的请求表示 是我没有数据要继续发送了,可以断开连接了,但是你如果还有数据可以继续向我发送数据. (2)接收方回复给发起方,表示接到了发起放的断开请求,开始着手准备断开事宜 (3)接收方准备完成后,给发起方发送一个标识,表示接受方没有数据继续发送了 可以断开连接了 (4)发起方接收到消息后,准备断开连接,回收资源 如果面试官问你,哪句代码体现了四次挥手? 回答: close() (重点)udp协议:快. 不安全,不可靠,不面向连接,面向数据包形式的传输
官方文档对socket模块下的socket.send()和socket.sendall()解释如下: socket.send(string[, flags]) Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for recv() above. Returns the number of bytes sent. Applications are responsible for checking that all data has been sent; if only some of the data was transmitted, the application needs to attempt delivery of the remaining data. send()的返回值是发送的字节数量,这个数量值可能小于要发送的string的字节数,也就是说可能无法发送string中所有的数据。如果有错误则会抛出异常。 – socket.sendall(string[, flags]) Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for recv() above. Unlike send(), this method continues to send data from string until either all data has been sent or an error occurs. None is returned on success. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent. 尝试发送string的所有数据,成功则返回None,失败则抛出异常。 故,下面两段代码是等价的: #sock.sendall('Hello world\n') #buffer = 'Hello world\n' #while buffer: # bytes = sock.send(buffer) # buffer = buffer[bytes:] send和sendall方法