socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口,在设计模式中,socket就是一个门面模式,他把复杂的的TCP/IP协议族隐藏在socket接口后面。
IP是用来标识互联网中的一台主机位置,port是用来标识这台机器上的一个应用程序,IP地址是配置到网卡上的。
pid是同一台机器上不同进程或线程的标识。
套接字被设计用在同一台主机上多个应用程序之间的通讯,也被称为进程间通讯或IPC,套接字分别有文件型或网络型。
tcp是基于链接的,必须先启动服务端,然后再启动客户端去链接服务端
tcp服务端
ss = socket() #创建服务器套接字
ss.bind() #把地址绑定到套接字
ss.listen() #监听链接
inf_loop: #服务器无限循环
cs = ss.accept() #接受客户端链接
comm_loop: #通讯循环
cs.recv()/cs.send() #对话(接收与发送)
cs.close() #关闭客户端套接字
ss.close() #关闭服务器套接字(可选)
基于TCP服务端的代码:
from socket import *
ip_port = ("127.0.0.1",8000)
back_log = 5
buffer_size = 1024
tcp_server = socket(AF_INET,SOCK_STREAM)
tcp_server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
tcp_server.bind(ip_port)
tcp_server.listen(back_log)
while True:
print("服务端开始运行了")
conn,addr = tcp_server.accept()
print("双向链接是",conn)
print("客户端地址是",addr)
while True:
try:
data = conn.recv(buffer_size)
print("收到客户端发来的消息是",data.decode("utf-8"))
conn.send(data.upper())
except Exception:
break
conn.close()
tcp_server.close()
tcp客户端
cs = socket() # 创建客户套接字
cs.connect() # 尝试连接服务器
comm_loop: # 通讯循环
cs.send()/cs.recv() # 对话(发送/接收)
cs.close() # 关闭客户套接字
基于TCP客户端1的代码
from socket import *
ip_port = ("127.0.0.1",8000)
buffer_size = 1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while True:
msg = input(">>:").strip()
if not msg:
continue
tcp_client.send(msg.encode("utf-8"))
print("客户端已经发送了消息")
data = tcp_client.recv(buffer_size)
print("收到服务端发来的消息是",data.decode("utf-8"))
tcp_client.close()
基于TCP客户端2的代码
from socket import *
ip_port = ("127.0.0.1",8000)
buffer_size = 1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while True:
msg = input(">>:").strip()
if not msg:
continue
tcp_client.send(msg.encode("utf-8"))
print("客户端已经发送了消息")
data = tcp_client.recv(buffer_size)
print("收到服务端发来的消息是",data.decode("utf-8"))
tcp_client.close()
udp是无链接的,先启动哪一端都不会报错
udp服务端
1 ss = socket(AF_INET,SOCK_DGRAM) #创建一个服务器的套接字
2 ss.bind() #绑定服务器套接字
3 inf_loop: #服务器无限循环
4数据,数据的地址= 套接字名.recvfrom(接收字节的长度)
5套接字名.sendto(数据,数据的地址)
基于UDP服务端代码:
from socket import *
ip_port = ("127.0.0.1",8008)
buffer_size = 1024
udp_server = socket(AF_INET,SOCK_DGRAM)
udp_server.bind(ip_port)
while True:
data,addr = udp_server.recvfrom(buffer_size)
print(data)
udp_server.sendto(data.upper(),addr)
udp客户端
设置服务端的地址加端口
设置字节长度
cs = socket() # 创建客户套接字
客户端套接字名.sendto(.编码,服务端地址加端口)
数据,数据的地址=客户端套接字名.recvfrom(定义的字节长度)
输出打印时解码
基于UDP客户端1代码
from socket import *
ip_port = ("127.0.0.1",8008)
buffer_size = 1024
udp_client = socket(AF_INET,SOCK_DGRAM)
while True:
msg = input(">>:")
udp_client.sendto(msg.encode("utf-8"),ip_port)
data,addr = udp_client.recvfrom(buffer_size)
print(data.decode("utf-8"))
基于UDP客户端2代码
from socket import *
ip_port = ("127.0.0.1",8008)
buffer_size = 1024
udp_client = socket(AF_INET,SOCK_DGRAM)
while True:
msg = input(">>:")
udp_client.sendto(msg.encode("utf-8"),ip_port)
data,addr = udp_client.recvfrom(buffer_size)
print(data.decode("utf-8"))
基于TCP远程执行命令服务端代码
from socket import *
import subprocess
ip_port = ("127.0.0.1",8002)
back_log = 5
buffer_size = 1024
tcp_server = socket(AF_INET,SOCK_STREAM)
tcp_server.bind(ip_port)
tcp_server.listen(back_log)
while True:
conn,addr = tcp_server.accept()
print("新的客户端链接是",addr)
while True:
try:
cmd = conn.recv(buffer_size)
print("收到客户端的命令",cmd)
res = subprocess.Popen(cmd.decode("utf-8"),shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
err = res.stderr.read()
if err:
cmd_res = err
else:
cmd_res = res.stdout.read()
conn.send(cmd_res)
except Exception as e :
print(e)
break
conn.close()
基于TCP远程执行命令客户端代码
from socket import *
ip_port = ("127.0.0.1",8002)
buffer_size = 1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while True:
cmd = input(">>:").strip()
if not cmd:
continue
if cmd == "quit":
break
tcp_client.send(cmd.encode("utf-8"))
cmd_res = tcp_client.recv(buffer_size)
print("命令的执行结果是",cmd_res.decode("gbk"))
tcp_client.close()
tcp和ufp的区别?
recv在自己这端的缓冲区为空时,阻塞。
recvfrom在自己这端的缓冲区为空时,就收一个空。
基于客户端发请求服务端返回时间的需求:
udp服务端代码:
from socket import *
import time
ip_port = ("127.0.0.1",8001)
buffer_size = 1024
udp_server = socket(AF_INET,SOCK_DGRAM)
udp_server.bind(ip_port)
while True:
data,addr = udp_server.recvfrom(buffer_size)
print(data)
if not data:
fmt = "%Y-%m-%d %X"
else:
fmt = data.decode("utf-8")
back_time = time.strftime(fmt)
udp_server.sendto(back_time.encode("utf-8"),addr)
udp_server.close()
udp客户端代码:
from socket import *
ip_port = ("127.0.0.1",8001)
buffer_size = 1024
udp_client = socket(AF_INET,SOCK_DGRAM)
while True:
msg = input(">>:").strip()
udp_client.sendto(msg.encode("utf-8"),ip_port)
data,addr = udp_client.recvfrom(buffer_size)
print("udp服务器的标准时间是",data.decode("utf-8"))
udp_client.close()
粘包现象:
tcp服务端代码:
from socket import *
import subprocess
ip_port = ("127.0.0.1",8000)
back_log = 5
buffer_size = 1024
tcp_server = socket(AF_INET,SOCK_STREAM)
tcp_server.bind(ip_port)
tcp_server.listen(back_log)
while True:
conn,addr = tcp_server.accept()
print("新客户端的链接是",addr)
while True:
try:
cmd = conn.recv(buffer_size)
if not cmd:
break
print("客户端的命令是",cmd)
res = subprocess.Popen(cmd.decode("utf-8"),shell= True,
stdin= subprocess.PIPE,
stdout= subprocess.PIPE,
stderr= subprocess.PIPE)
err = res.stderr.read()
if err:
cmd_res = err
else:
cmd_res = res.stdout.read()
if not cmd_res:
cmd_res ="执行成功".encode("gbk")
conn.send(cmd_res)
except Exception as e:
print(e)
break
tcp客户端代码:
from socket import *
ip_port = ("127.0.0.1",8000)
buffer_size= 1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while True:
msg = input(">>:").strip()
if not msg:
continue
if msg == "quit":
break
tcp_client.send(msg.encode("utf-8"))
cmd = tcp_client.recv(buffer_size)
print("收到服务端的命令是",cmd.decode("gbk"))
----------------------------------------------------------------------------------------------------
udp服务端代码:
from socket import *
import subprocess
ip_port = ("127.0.0.1",8000)
buffer_size = 1024
udp_server = socket(AF_INET,SOCK_DGRAM)
udp_server.bind(ip_port)
while True:
cmd,addr = udp_server.recvfrom(buffer_size)
res = subprocess.Popen(cmd.decode("utf-8"),shell= True,
stdin= subprocess.PIPE,
stdout= subprocess.PIPE,
stderr= subprocess.PIPE)
err = res.stderr.read()
if err:
cmd_res = err
else:
cmd_res = res.stdout.read()
if not cmd_res:
cmd_res = "执行成功".encode("gbk")
udp_server.sendto(cmd_res,addr)
udp客户端代码:
from socket import *
ip_port = ("127.0.0.1",8000)
buffer_size= 1024
udp_client = socket(AF_INET,SOCK_DGRAM)
udp_client.connect(ip_port)
while True:
msg = input(">>:").strip()
if not msg:
continue
if msg == "quit":
break
udp_client.sendto(msg.encode("utf-8"),ip_port)
cmd_res,addr = udp_client.recvfrom(buffer_size)
print("收到服务端的命令是",cmd_res.decode("gbk"))
udp_client.close()
----------------------------------------------------------------------------------------------------
正常关闭用if not处理、非正常关闭用try处理tcp服务端的代码:
from socket import *
import subprocess
ip_port = ("127.0.0.1",8001)
back_log = 5
buffer_size = 1024
tcp_server = socket(AF_INET,SOCK_STREAM)
tcp_server.bind(ip_port)
tcp_server.listen(back_log)
while True:
conn,addr = tcp_server.accept()
print("新客户端的衔接是",conn)
while True:
try:
cmd = conn.recv(buffer_size)
if not cmd:
break
print("收到的命令是",cmd)
res = subprocess.Popen(cmd.decode("utf-8"),shell= True,
stdout= subprocess.PIPE,
stdin= subprocess.PIPE,
stderr= subprocess.PIPE)
err = res.stderr.read()
if err:
cmd_res = err
else:
cmd_res = res.stdout.read()
if not cmd_res:
cmd_res = "执行成功".encode("gbk")
conn.send(cmd_res)
except Exception as e:
print(e)
break
tcp客户端的代码:
from socket import *
ip_port = ("127.0.0.1",8000)
buffer_size = 1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while True:
msg = input(">>:").strip()
if not msg:
continue
if msg == "quit":
break
tcp_client.send(msg.encode("utf-8"))
cmd = tcp_client.recv(buffer_size)
print("收到服务端命令是",cmd.decode("gbk"))
tcp粘包解决办法:
发送端可以是一K一K地发送数据,而接收端的应用程序可以两K两K地提走数据,当然也有可能一次提走3K或6K数据,或者一次只提走几个字节的数据,也就是说,应用程序所看到的数据是一个整体,或说是一个流(stream),一条消息有多少字节对应用程序是不可见的,因此TCP协议是面向流的协议,这也是容易出现粘包问题的原因。而UDP是面向消息的协议,每个UDP段都是一条消息,应用程序必须以消息为单位提取数据,不能一次提取任意字节的数据,这一点和TCP是很不同的。怎样定义消息呢?可以认为对方一次性write/send的数据为一个消息,需要明白的是当对方send一条信息的时候,无论底层怎样分段分片,TCP协议层会把构成整条消息的数据段排序完成后才呈现在内核缓冲区。
例如基于tcp的套接字客户端往服务端上传文件,发送时文件内容是按照一段一段的字节流发送的,在接收方看了,根本不知道该文件的字节流从何处开始,在何处结束
所谓粘包问题主要还是因为接收方不知道消息之间的界限,不知道一次性提取多少字节的数据所造成的。
此外,发送方引起的粘包是由TCP协议本身造成的,TCP为提高传输效率,发送方往往要收集到足够多的数据后才发送一个TCP段。若连续几次需要send的数据都很少,通常TCP会根据优化算法把这些数据合成一个TCP段后一次发送出去,这样接收方就收到了粘包数据。
- TCP(transport control protocol,传输控制协议)是面向连接的,面向流的,提供高可靠性服务。收发两端(客户端和服务器端)都要有一一成对的socket,因此,发送端为了将多个发往接收端的包,更有效的发到对方,使用了优化方法(Nagle算法),将多次间隔较小且数据量小的数据,合并成一个大的数据块,然后进行封包。这样,接收端,就难于分辨出来了,必须提供科学的拆包机制。 即面向流的通信是无消息保护边界的。
- UDP(user datagram protocol,用户数据报协议)是无连接的,面向消息的,提供高效率服务。不会使用块的合并优化算法,, 由于UDP支持的是一对多的模式,所以接收端的skbuff(套接字缓冲区)采用了链式结构来记录每一个到达的UDP包,在每个UDP包中就有了消息头(消息来源地址,端口等信息),这样,对于接收端来说,就容易进行区分处理了。 即面向消息的通信是有消息保护边界的。
- tcp是基于数据流的,于是收发的消息不能为空,这就需要在客户端和服务端都添加空消息的处理机制,防止程序卡住,而udp是基于数据报的,即便是你输入的是空内容(直接回车),那也不是空消息,udp协议会帮你封装上消息头
tcp粘包解决办法服务端代码:
from socket import *
import subprocess
import struct
ip_port = ("127.0.0.1",8000)
back_log = 5
buffer_size = 1024
tcp_server = socket(AF_INET,SOCK_STREAM)
tcp_server.bind(ip_port)
tcp_server.listen(back_log)
while True:
conn,addr = tcp_server.accept()
print("新客户端的链接是",addr)
while True:
try:
cmd = conn.recv(buffer_size)
if not cmd:
break
print("收到客户端的命令是",cmd)
res = subprocess.Popen(cmd.decode("utf-8"),shell= True,
stdin= subprocess.PIPE,
stdout= subprocess.PIPE,
stderr= subprocess.PIPE)
err = res.stderr.read()
if err:
cmd_res = res.stderr.read()
else:
cmd_res = res.stdout.read()
if not cmd_res:
cmd_res = "执行成功".encode("gbk")
length = len(cmd_res)
data_length = struct.pack("i",length)
conn.send(data_length)
conn.send(cmd_res)
except Exception as e:
print(e)
break
tcp粘包解决办法客户端代码:
from socket import *
import struct
from functools import partial
ip_port = ("127.0.0.1",8000)
buffer_size= 1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while True:
cmd = input(">>:").strip()
if not cmd:
continue
if cmd == "quit":
break
tcp_client.send(cmd.encode("utf-8"))
length_data = tcp_client.recv(4)
length = struct.unpack("i",length_data)[0]
recv_size = 0
recv_data = b""
while recv_size < length:
recv_data += tcp_client.recv(buffer_size)
recv_size = len(recv_data)
print("收到服务端的命令是",recv_data.decode("gbk"))
tcp_client.close()
socketserver实现并发服务端代码:
import socketserver
class MyServer(socketserver.BaseRequestHandler):
def handle(self):
print("conn is:",self.request)
print("addr is:",self.client_address)
while True:
try:
data = self.request.recv(1024)
if not data:
break
print("收到客户端的命令是",data,self.client_address)
self.request.sendall(data.upper())
except Exception as e:
print(e)
break
if __name__ == '__main__':
s = socketserver.ThreadingTCPServer(("127.0.0.1",8000),MyServer)
s.serve_forever()
socketserver实现并发客户端1代码:
from socket import *
ip_port = ("127.0.0.1",8000)
buffer_size = 1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while True:
msg = input(">>:").strip()
if not msg:
continue
if msg == "quit":
break
tcp_client.send(msg.encode("utf-8"))
data = tcp_client.recv(buffer_size)
print("收到服务端的命令",data.decode("utf-8"))
tcp_client.close()
socketserver实现并发客户端2代码:
from socket import *
ip_port = ("127.0.0.1",8000)
buffer_size = 1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while True:
msg = input(">>:").strip()
if not msg:
continue
if msg == "quit":
break
tcp_client.send(msg.encode("utf-8"))
data = tcp_client.recv(buffer_size)
print("收到服务端的命令",data.decode("utf-8"))
tcp_client.close()
认证客户端的链接合法性需求:
服务端代码:
from socket import *
import hmac,os
secret_key=b'linhaifeng bang bang bang'
def conn_auth(conn):
'''
认证客户端链接
:param conn:
:return:
'''
print('开始验证新链接的合法性')
msg=os.urandom(32)
conn.sendall(msg)
h=hmac.new(secret_key,msg)
digest=h.digest()
respone=conn.recv(len(digest))
return hmac.compare_digest(respone,digest)
def data_handler(conn,bufsize=1024):
if not conn_auth(conn):
print('该链接不合法,关闭')
conn.close()
return
print('链接合法,开始通信')
while True:
data=conn.recv(bufsize)
if not data:break
conn.sendall(data.upper())
def server_handler(ip_port,bufsize,backlog=5):
'''
只处理链接
:param ip_port:
:return:
'''
tcp_socket_server=socket(AF_INET,SOCK_STREAM)
tcp_socket_server.bind(ip_port)
tcp_socket_server.listen(backlog)
while True:
conn,addr=tcp_socket_server.accept()
print('新连接[%s:%s]' %(addr[0],addr[1]))
data_handler(conn,bufsize)
if __name__ == '__main__':
ip_port=('127.0.0.1',8000)
bufsize=1024
server_handler(ip_port,bufsize)
客户端代码:
from socket import *
import hmac,os
secret_key=b'linhaifeng bang bang bang'
def conn_auth(conn):
'''
验证客户端到服务器的链接
:param conn:
:return:
'''
msg=conn.recv(32)
h=hmac.new(secret_key,msg)
digest=h.digest()
conn.sendall(digest)
def client_handler(ip_port,bufsize=1024):
tcp_socket_client=socket(AF_INET,SOCK_STREAM)
tcp_socket_client.connect(ip_port)
conn_auth(tcp_socket_client)
while True:
data=input('>>: ').strip()
if not data:continue
if data == 'quit':break
tcp_socket_client.sendall(data.encode('utf-8'))
respone=tcp_socket_client.recv(bufsize)
print(respone.decode('utf-8'))
tcp_socket_client.close()
if __name__ == '__main__':
ip_port=('127.0.0.1',8000)
bufsize=1024
client_handler(ip_port,bufsize)