前言
Python 提供 socket 模块用于访问网络服务,使得不同主机之间的进程可以相互通信
正文
1、Socket 对象的创建
socket.socket([family[, type[, proto]]])
family:套接字家族,一般取值如下:
socket.AF_INET :不同主机上的网络通信,使用 IPV4 协议
socket.AF_INET6:不同主机上的网络通信,使用 IPV6 协议
socket.AF_UNIX :同一主机上的进程通信
type:套接字类型,一般取值如下:
socket.SOCK_STREAM:流套接字,用于 TCP 通信
socket.SOCK_DGRAM :数据报套接字,用于 UDP 通信
socket.SOCK_RAW :原始套接字,用于处理 ICMP、IGMP 等特殊的网络报文
proto:协议编号,默认为 0
2、Socket 对象的常用方法
(1)服务端的常用方法
bind(address):绑定地址到套接字,address 表示通信地址,格式取决于使用的套接字家族
listen(backlog):监听连接,backlog 表示在拒绝连接前可以挂起的最大连接数
accept():接受连接,返回 (conn, address),conn 是新的 Socket 对象,用来接收和发送数据
(2)客户端的常用方法
connect(address):连接到指定地址的套接字,如果连接出错,返回 socket.error
connect_ex(address):连接到指定地址的套接字,如果连接出错,返回 错误编码
(3)公共用途常用方法
close():关闭套接字
recv(bufsize[,flag]):接收数据,一般用于 TCP 协议,返回接收到的数据
recvfrom(bufsize[,flag]):接收数据,一般用于 UDP 协议,返回接收到的数据和发送方的地址
send(data[,flag]):发送数据,一般用于 TCP 协议,返回发送的字节数
sendto(data[,flag],address):发送数据,一般用于 UDP 协议,返回发送的字节数
getsockname():返回连接套接字的己方地址
getpeername():返回连接套接字的远程地址
getsockopt(level,optname):获取套接字选项
setsockopt(level,optname,value):设置套接字选项
gettimeout():获取套接字的超时值,如果没有设置超时时间,返回 None
settimeout(timeout):设置套接字的超时值,timeout 表示超时时间,单位为秒
3、例子
(1)客户端传输数据到服务端
# server.py import socket import threading def create_service(): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) address = ('127.0.0.1', 8888) server.bind(address) server.listen(5) print('Waiting connection ...') while True: conn, addr = server.accept() thread = threading.Thread(target = handle_request, args = (conn, addr)) thread.start() server.close() def handle_request(conn, addr): print('Accept connection from', addr) while True: recv_byte = conn.recv(1024) recv_data = recv_byte.decode('UTF-8') print(recv_data) if recv_data == 'quit': break conn.close() if __name__ == '__main__': create_service()
# client.py import socket client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) address = ('127.0.0.1', 8888) client.connect(address) print('Connect to', address) while True: send_data = input('Please Enter Something: ') send_byte = send_data.encode('UTF-8') client.send(send_byte) if send_data == 'quit': break client.close()
(2)客户端从服务端下载文件
# server.py import socket import threading import os import json def create_service(): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) address = ('127.0.0.1', 8888) server.bind(address) server.listen(5) print('Waiting connection ...') while True: conn, addr = server.accept() thread = threading.Thread(target = handle_request, args = (conn, addr)) thread.start() server.close() def handle_request(conn, addr): print('Accept connection from', addr) while True: file_name = conn.recv(1024).decode('UTF-8') print('Request File:', file_name) if not os.path.exists(file_name): message = 'Not Exist' print(message) conn.send(message.encode('UTF-8')) else: real_name = os.path.split(file_name)[1] file_size = os.path.getsize(file_name) file_info = json.dumps({ 'real_name': real_name, 'file_size': file_size }) conn.send(file_info.encode('UTF-8')) conn.recv(1024) print('Send File ...') with open(file_name, 'rb') as file: for line in file: conn.send(line) print('Send File Successful') conn.close() if __name__ == '__main__': create_service()
# client.py import socket import os import json client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) address = ('127.0.0.1', 8888) client.connect(address) print('Connect to', address) while True: file_name = input('Please Enter File Name: ') client.send(bytes(file_name, encoding='UTF-8')) recv_data = client.recv(1024).decode('UTF-8') if recv_data == 'Not Exist': print(recv_data) else: file_info = json.loads(recv_data) real_name = file_info['real_name'] file_size = file_info['file_size'] client.send(bytes('Received', encoding='UTF-8')) print('Receive File ...') recv_size = 0 with open(real_name, 'wb') as file: while recv_size < file_size: chunk = client.recv(1024) file.write(chunk) recv_size += len(chunk) print('Receive File Successful') client.close()
(3)客户端上传文件到服务端
# server.py import socket import threading import os import json def create_service(): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) address = ('127.0.0.1', 8888) server.bind(address) server.listen(5) print('Waiting connection ...') while True: conn, addr = server.accept() thread = threading.Thread(target = handle_request, args = (conn, addr)) thread.start() server.close() def handle_request(conn, addr): print('Accept connection from', addr) while True: recv_data = conn.recv(1024).decode('UTF-8') file_info = json.loads(recv_data) real_name = file_info['real_name'] file_size = file_info['file_size'] conn.send(bytes('Received', encoding='UTF-8')) print('Receive File ...') recv_size = 0 with open(real_name, 'wb') as file: while recv_size < file_size: chunk = conn.recv(1024) file.write(chunk) recv_size += len(chunk) print('Receive File Successful') conn.close() if __name__ == '__main__': create_service()
# client.py import socket import os import json client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) address = ('127.0.0.1', 8888) client.connect(address) print('Connect to', address) while True: file_name = input('Please Enter File Name: ') if not os.path.exists(file_name): print('Not Exist') continue real_name = os.path.split(file_name)[1] file_size = os.path.getsize(file_name) file_info = json.dumps({ 'real_name': real_name, 'file_size': file_size }) client.send(file_info.encode('UTF-8')) client.recv(1024) print('Send File ...') with open(file_name, 'rb') as file: for line in file: client.send(line) print('Send File Successful') client.close()
文章知识点与官方知识档案匹配,可进一步学习相关知识