标签:from OSI七层 udp协议 不同 长度 recv 本质 port 颜色
TCP
# server #client
import socket import socket
sk = socket.socket() sk = socket.socket()
sk.bind((‘127.0.0.1‘, 9000))
sk.listen()
conn, adr = sk.accept() sk = connect((‘127.0.0.1‘, 9000))
msg = conn.recv(1024) sk.send(bytes)
msg.decode(‘utf-8‘)
conn.send(bytes) msg = sk.recv(1024).decode(‘utf-8‘)
conn.close() sk.close()
sk.close()
UDP
# server # client
import socket import socket
sk = socket.socket(type=socket.SOCK_DGRAM) sk = socket.socket(type=socket.SOCK_DGRAM)
sk.bind((‘127.0.0.1‘, 9000))
msg, adr = sk.recvfrom(1024) sk.sendto(bytes, (‘127.0.0.1‘, 9000))
msg.decode(‘utf-8‘)
sk.sendto(bytes, adr) msg = sk.recv(1024).decode(‘utf-8‘)
sk.close() sk.close()
‘我是好人‘ → 字节
计算机上的存储/网线上的数据传输 → 二进制
1位 = 1bit
10101100 8位 = 8bit = 1个字节
s1编码 | s2编码 | utf-8 | gbk |
---|---|---|---|
1 → 我 | 10 → 我 | ||
2 → 是 | 11 → 是 | ||
3 → 好 | 12 → 好 | ||
4 → 人 | 14 → 人 |
s1编码
‘我是好人‘ → 1234 → 00000001 00000010 00000011 00000100
将字符串按照指定编码编译后才能转成二进制数据进行存储和传输
s2编码
‘我是好人‘ → 10121314 → 00001010 00001011 00001100 00001101
将字符串按照指定编码编译后才能转成二进制数据进行存储和传输
send
recv
基于TCP协议,实现 Server端 和 Client端 连接后,知道对面 Client端 是哪一个好友,且不同好友聊天显示不同颜色字体。
# Server.py
import socket
import json
sk = socket.socket()
sk.bind((‘127.0.0.1‘, 9000))
sk.listen()
color_dic = {
‘123‘:{‘color‘:‘\033[31m‘, ‘name‘:‘alex‘},
‘456‘:{‘color‘:‘\033[32m‘, ‘name‘:‘wusir‘},
‘789‘:{‘color‘:‘\033[33m‘, ‘name‘:‘yuan‘}
}
while True:
conn, _ = sk.accept()
while True:
msg = conn.recv(1024).decode(‘utf-8‘)
dic = json.loads(msg)
user_id = dic[‘id‘]
if dic[‘msg‘].upper() == ‘Q‘:
print(‘%s已经下线‘%(color_dic[user_id][‘name‘],))
break
print(‘%s%s : %s\033[0m‘%(color_dic[user_id][‘color‘],color_dic[user_id][‘name‘],dic[‘msg‘]))
inp = input(‘>>>‘)
conn.send(inp.encode(‘utf-8‘))
if inp.upper() == ‘Q‘:
print(‘您已经断开和%s的聊天‘%(color_dic[user_id][‘name‘],))
break
conn.close()
sk.close()
# Client.py
import socket
import json
sk = socket.socket()
user_id = ‘123‘
sk.connect((‘127.0.0.1‘, 9000))
while True:
inp = input(‘>>>‘)
dic = {‘msg‘:inp, ‘id‘:user_id}
str_dic = json.dumps(dic)
sk.send(str_dic.encode(‘utf-8‘))
if inp.upper() == ‘Q‘:
print(‘您已经断开和server的聊天‘)
break
msg = sk.recv(1024).decode(‘utf-8‘)
if msg.upper() == ‘Q‘:
break
print(msg)
sk.close()
import struct
ret = struct.pack(‘i‘, 1)
print(ret)
ret = struct.pack(‘i‘, 6666)
print(ret)
ret = struct.pack(‘i‘, 7878)
print(ret)
ret = struct.pack(‘i‘, 100000)
print(ret)
print(struct.unpack(‘i‘,ret)) # unpack返回的是元组
import time
import struct
import socket
sk = socket.socket()
sk.bind((‘127.0.0.1‘, 9000))
sk.listen()
conn, _ = sk.accept()
time.sleep(0.1)
byte_len = conn.recv(4)
size = struct.unpack(‘i‘, byte_len)[0] # unpack返回的是元组
msg1 = conn.recv(size)
print(msg1)
byte_len = conn.recv(4)
size = struct.unpack(‘i‘, byte_len)[0]
msg2 = conn.recv(size)
print(msg2)
conn.close()
import struct
import socket
sk = socket.socket()
sk.connect((‘127.0.0.1‘, 9000))
msg = b‘hellow‘
byte_len = struct.pack(‘i‘, len(msg))
sk.send(byte_len)
sk.send(msg)
msg = b‘world‘
byte_len = struct.pack(‘i‘, len(msg))
sk.send(byte_len)
sk.send(msg)
sk.close()
标签:from OSI七层 udp协议 不同 长度 recv 本质 port 颜色
原文地址:https://www.cnblogs.com/elliottwave/p/12639878.html