1:socket:中文称为套接字
2:线程之间的通信有很多种形式,例如event事件,lock锁,信号量,queue队列等
3:进程之间的通信(IPC: Inter-Process Communication),一般使用套接字,当然还有其他的通信方式,套接字的IPC方式使得跨平台之间的进程通信成为可能。最早的socket是在BSD-Unix平台上发布,最终成为了行业标准,使得计算机之间的通信变得非常简单
socket.AF_UNIX 用于同一台机器上的进程通信(既本机通信)
socket.AF_INET 用于服务器与服务器之间的网络通信
socket.AF_INET6 基于IPV6方式的服务器与服务器之间的网络通信
socket.SOCK_STREAM 基于TCP的流式socket通信
socket.SOCK_DGRAM 基于UDP的数据报式socket通信
socket.SOCK_RAW 原始套接字,普通的套接字无法处理ICMP、IGMP等网络报文,而SOCK_RAW可以;其次SOCK_RAW也可以处理特殊的IPV4报文;此外,利用原始套接字,可以通过IP_HDRINCL套接字选项由用户构造IP头
socket.SOCK_SEQPACKET 可靠的连续数据包服务
TCP服务端代码示例
import socket
from pprint import pprint
# 创建TCP连接
socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_instance.bind(('127.0.0.1', 9000))
# 操作系统可以挂起的最大连接数,如果同一时间的连接数超过5,拒绝其他的连接
socket_instance.listen(5)
# 循环接收新的客户端连接
while True:
# 接收客户端的请求,且获取新socket对象和客户端信息
new_socket, client_addr = socket_instance.accept()
# 循环接收已连接的客户端发送的数据
while True:
# 从缓存区中读取1024字节信息
data = new_socket.recv(1024).decode()
# 返回客户端的一下信息
pprint(data)
# 返回客户端地址 ('127.0.0.1', 65148)
pprint(client_addr)
# 发送数据
new_socket.sendall('服务器已收到您的消息'.encode())
TCP客户端代码实现
import socket
# 创建TCP连接
socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_instance.connect(('127.0.0.1', 9000))
while True:
cmd = input("请输入您想说的话:")
socket_instance.send(cmd.encode())
# 服务器返回信息
data = socket_instance.recv(1024)
print(data)
UDP服务器端代码实现
import socket
# 创建UDP连接
socket_instance = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket_instance.bind(('127.0.0.1', 9000))
# 循环接收新的客户端连接
while True:
# 接收客户端的请求,且获取新socket对象和客户端信息
data, client_addr = socket_instance.recvfrom(1024)
print(data.decode())
socket_instance.sendto('服务器已收到您的消息'.encode(), client_addr)
UDP客户端代码实现
import socket
# 创建socket实例
socket_instance = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
data = input("请输入您想说的话:")
socket_instance.sendto(data.encode(), ('127.0.0.1', 9000))
data, server_addr = socket_instance.recvfrom(1024)
print(data.decode())
线程级别的TCPServer
import threading
import logging
import socket
from pprint import pprint
logging.basicConfig(level=logging.INFO, format='%(message)s', filename='./app.log')
def showThread():
while not threading.Event().wait(3):
logging.info(threading.enumerate())
logging.info(threading.active_count())
class ChatServer:
# 初始化:绑定端口
def __init__(self, ip='127.0.0.1', port=9000):
self.socket = socket.socket()
self.addr = (ip, port)
self.event = threading.Event()
self.clients = {}
def start(self):
# 绑定地址,监听地址
self.socket.bind(self.addr)
self.socket.listen(10)
# 将阻塞的accept()方法放到线程执行
threading.Thread(target=self.__accept, name='accept').start()
def __accept(self):
# 阻塞的accept()方法,只要请求进来,就响应
while not self.event.is_set():
try:
conn, addr= self.socket.accept()
self.clients[addr] = conn
threading.Thread(target=self.__receive, args=(addr,), name='receive').start()
pprint(self.clients)
except Exception as e:
print(e)
def __receive(self, addr):
while not self.event.is_set():
conn = self.clients[addr]
try:
data = conn.recv(1024).decode()
except Exception as e:
data = 'quit'
print(data)
if data.strip() == 'quit':
print('~~~~~~~~~~~quit')
conn.close()
self.clients.pop(addr)
break
pprint(self.clients)
# 这里改下可以变为群聊
if addr in self.clients.keys():
try:
conn.send('回复{}的请求\n'.format(addr).encode())
except Exception as e:
print(e)
def stop(self):
self.event.set()
conn_list = self.clients.values()
for conn in conn_list:
conn.close()
self.event.wait(1)
try:
self.socket.close()
except ConnectionAbortedError as e:
print(e)
if __name__ == '__main__':
# 2 初始化ChatServer
chatServer = ChatServer()
# 3:用户循环输入数据
while True:
cmd = input('请输入命令: ').strip()
if cmd == 'start':
chatServer.start()
print('chatServer is start')
elif cmd == 'stop':
chatServer.stop()
print('chatServer is stop')
break
else:
print('请输入start or stop')
TCP Client客户端
import socket
# 创建TCP连接
socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_instance.connect(('127.0.0.1', 9000))
while True:
cmd = input("请输入您想说的话:")
socket_instance.send(cmd.encode())
data = socket_instance.recv(1024)
print(data.decode())