网络编程
2021-02-23 10:12:31 19 举报
AI智能生成
python网络编程相关
作者其他创作
大纲/内容
socketserver
client端
import socket
import time
sk = socket.socket()
sk.connect(('127.0.0.1',9000))
while True:
sk.send('hello'.encode('utf-8'))
msg = sk.recv(1024).decode('utf-8')
print(msg)
time.sleep(2)
sk.close()
import time
sk = socket.socket()
sk.connect(('127.0.0.1',9000))
while True:
sk.send('hello'.encode('utf-8'))
msg = sk.recv(1024).decode('utf-8')
print(msg)
time.sleep(2)
sk.close()
server端
import socketserver
class MyServer(socketserver.BaseRequestHandler):
def handle(self):
conn = self.request
while True:
try:
msg = conn.recv(1024).decode('utf-8')
print(msg)
MSG = msg.upper()
conn.send(MSG.encode('utf-8'))
except ConnectionResetError:
pass
server = socketserver.ThreadingTCPServer(('127.0.0.1',9000),MyServer)
server.serve_forever()
class MyServer(socketserver.BaseRequestHandler):
def handle(self):
conn = self.request
while True:
try:
msg = conn.recv(1024).decode('utf-8')
print(msg)
MSG = msg.upper()
conn.send(MSG.encode('utf-8'))
except ConnectionResetError:
pass
server = socketserver.ThreadingTCPServer(('127.0.0.1',9000),MyServer)
server.serve_forever()
TCP协议粘包问题
1.recv(1024)不一定接收到的是1024个字节,是最多接收1024个字节
2、两条连续发送的数据,一定要注意粘包问题
3、先发送数据的长度,在发送内容
4、接收端先接收4个字节后,在根据解析到的内容长度进行接收
代码
#server端
import json
import struct
import socket
# 接收
sk = socket.socket()
sk.bind(('127.0.0.1',9001))
sk.listen()
conn,_ =sk.accept()
msg_len = conn.recv(4)
dic_len = struct.unpack('i',msg_len)[0]
msg = conn.recv(dic_len).decode('utf-8')
msg = json.loads(msg)
with open(msg['filename'],'wb') as f:
while msg['filesize'] > 0:
content = conn.recv(1024)
msg['filesize'] -= len(content)
f.write(content)
conn.close()
sk.close()
#client
import os
import json
import struct
import socket
# 发送
sk = socket.socket()
# sk.connect(('192.168.14.109',9012))
sk.connect(('127.0.0.1',9001))
# 文件名\文件大小
abs_path = r'D:\python22期\day28 课上视频\3.网络基础概念.mp4'
filename = os.path.basename(abs_path)
filesize = os.path.getsize(abs_path)
dic = {'filename':filename,'filesize':filesize}
str_dic = json.dumps(dic)
b_dic = str_dic.encode('utf-8')
mlen = struct.pack('i',len(b_dic)) #len(b_dic)必须是bytes类型的
sk.send(mlen) # 4个字节 表示字典转成字节之后的长度
sk.send(b_dic) # 具体的字典数据
with open(abs_path,mode = 'rb') as f:
while filesize>0:
content = f.read(1024)
filesize -= len(content)
sk.send(content)
sk.close()
import json
import struct
import socket
# 接收
sk = socket.socket()
sk.bind(('127.0.0.1',9001))
sk.listen()
conn,_ =sk.accept()
msg_len = conn.recv(4)
dic_len = struct.unpack('i',msg_len)[0]
msg = conn.recv(dic_len).decode('utf-8')
msg = json.loads(msg)
with open(msg['filename'],'wb') as f:
while msg['filesize'] > 0:
content = conn.recv(1024)
msg['filesize'] -= len(content)
f.write(content)
conn.close()
sk.close()
#client
import os
import json
import struct
import socket
# 发送
sk = socket.socket()
# sk.connect(('192.168.14.109',9012))
sk.connect(('127.0.0.1',9001))
# 文件名\文件大小
abs_path = r'D:\python22期\day28 课上视频\3.网络基础概念.mp4'
filename = os.path.basename(abs_path)
filesize = os.path.getsize(abs_path)
dic = {'filename':filename,'filesize':filesize}
str_dic = json.dumps(dic)
b_dic = str_dic.encode('utf-8')
mlen = struct.pack('i',len(b_dic)) #len(b_dic)必须是bytes类型的
sk.send(mlen) # 4个字节 表示字典转成字节之后的长度
sk.send(b_dic) # 具体的字典数据
with open(abs_path,mode = 'rb') as f:
while filesize>0:
content = f.read(1024)
filesize -= len(content)
sk.send(content)
sk.close()
OS模型
五层模型
应用层
会话层
表示层
传输层
port端口 四层交换机
网络层
ipv4 ipv6 三层交换机
数据链路层
arp协议 :地址解析协议
根据IP地址获取对应的mac地址
网卡 二层交换机
物理层
七层模型
应用层
表示层
会话层
传输层
网络层
数据链路层
物理层
TCP协议
三次握手
- 客户端发送syn请求,
2. 服务端接收客户端请求,回复ACK,并发送syn给客户端
3.客户端接收服务端请求,回复ACK,表示建立连接成功
4.三次握手分别由服务端的accept 与客户端的connect完成
四次挥手
1.服务端发送fin请求给客户端
2.客户端接收服务端断开连接请求,回复ACK给服务端
3.客户端先把未上传成功的数据发送完成后,再发送fin请求给服务端
4.服务端接收客户端请求后,回复ACK给客户端,服务端断开连接
5。四次挥手分别由服务端的conn.close 与客户端的sk.cose完成
server端
import socket
sk = socket.socket()
sk.bind(('127.0.0.1',9001))
sk.listen()
while True:
conn,_ = sk.accept()
while True:
msg = conn.recv(1024).decode('utf-8')
MSG = msg.upper()
conn.send(MSG.encode('utf-8'))
conn.close()
sk.close()
sk = socket.socket()
sk.bind(('127.0.0.1',9001))
sk.listen()
while True:
conn,_ = sk.accept()
while True:
msg = conn.recv(1024).decode('utf-8')
MSG = msg.upper()
conn.send(MSG.encode('utf-8'))
conn.close()
sk.close()
bind
绑定一个端口
listen
监听端口
accept
接收客户端的连接
三次握手
返回一个conn
返回一个客户端地址
conn.close
关闭与客户端之间的连接
四次挥手
sk.close
关闭服务端
client端
import socket
sk = socket.socket()
sk.connect(('127.0.0.1',9001))
while True:
msg = input('请输入你要发送的内容:').strip()
sk.send(msg.encode('utf-8'))
msg = sk.recv(1024).decode('utf-8')
print(msg)
sk.close()
sk = socket.socket()
sk.connect(('127.0.0.1',9001))
while True:
msg = input('请输入你要发送的内容:').strip()
sk.send(msg.encode('utf-8'))
msg = sk.recv(1024).decode('utf-8')
print(msg)
sk.close()
connect
连接服务端
三次握手
sk.close
关闭服务端
四次挥手
优点:
可靠的全双工连接
效率低
粘包问题
struck模块
struck.pack('i',json的长度)
struck.unpack('i',msg)返回的是一个元组,长度在元组的0位置上
import struct
s = 'fhdakjdhjka'
ret = struct.pack('i',len(s))
print(ret)
res = struct.unpack('i',ret) #返回的是一个个元组
print(res)
print(res[0])
s = 'fhdakjdhjka'
ret = struct.pack('i',len(s))
print(ret)
res = struct.unpack('i',ret) #返回的是一个个元组
print(res)
print(res[0])
UDP协议
server端
import socket
sk = socket.socket(type= socket.SOCK_DGRAM)
sk.bind(('127.0.0.1',9001))
while True:
msg,addr = sk.recvfrom(1024)
MSG = msg.decode('utf-8')
print(MSG)
content = input('请输入你要发送的内容:').strip()
sk.sendto(content.encode('utf-8'),addr)
sk.close()
sk = socket.socket(type= socket.SOCK_DGRAM)
sk.bind(('127.0.0.1',9001))
while True:
msg,addr = sk.recvfrom(1024)
MSG = msg.decode('utf-8')
print(MSG)
content = input('请输入你要发送的内容:').strip()
sk.sendto(content.encode('utf-8'),addr)
sk.close()
client端
import socket
sk = socket.socket(type = socket.SOCK_DGRAM)
server_id = ('127.0.0.1',9001)
while True:
msg = input('请输入你要发送的内容:').strip()
sk.sendto(msg.encode('utf-8'),server_id)
msg = sk.recv(1024).decode('utf-8')
print(msg)
sk.close()
sk = socket.socket(type = socket.SOCK_DGRAM)
server_id = ('127.0.0.1',9001)
while True:
msg = input('请输入你要发送的内容:').strip()
sk.sendto(msg.encode('utf-8'),server_id)
msg = sk.recv(1024).decode('utf-8')
print(msg)
sk.close()
优点
效率高
不可靠的连接
无连接
0 条评论
下一页