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()
:接受连接,返回(connect, address)
,connect 是新的 Socket 对象,用来接收和发送数据
(2)客户端的常用方法
connect(address)
:连接到指定地址的套接字,如果连接出错,返回socket.error
connect_ex(address)
:连接到指定地址的套接字,如果连接出错,返回错误编码
(3)公共用途常用方法
recv(bufsize[,flag])
:从套接字接收数据,一般用于 TCP 协议,返回接收到的数据recvfrom(bufsize[.flag])
:从套接字接收数据,一般用于 UDP 协议,返回接收到的数据和发送方的地址send(data[,flag])
:发送数据到套接字,一般用于 TCP 协议,返回发送的字节数sendall(data[,flag])
:完整发送数据到套接字,成功则返回 None,失败则抛出异常sendto(data[,flag], address)
:发送数据到套接字,一般用于 UDP 协议,返回发送的字节数close()
:关闭套接字getsockname()
:返回连接套接字的己方地址getpeername()
:返回连接套接字的远程地址setsockopt(level,optname,value)
:设置套接字选项的值getsockopt(level,optname)
:获取套接字选项的值settimeout(timeout)
:设置套接字的超时值,timeout 表示超时时间,单位为秒gettimeout()
:获取套接字的超时值,如果没有设置超时时间,返回 None
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()
【 阅读更多 Python 系列文章,请看 Python学习笔记 】