- TCP
服务器
import socket
# 1.创建socket对象 s
# 2.绑定本机IP地址,创建端口号 s.bind()
# 3.监听端口号 a.listen()
# 4.等待接收客户端请求,获取客户端IP地址和创建新的socket对象 conn, address = s.accept()
# 5.接收客户端发送的信息 data = conn.recv(1024) 接收的是字节序列要转码str decode()
# 6.发送信息给客户端 conn,send(str.encode()) 发送的str要转字节序列
# 7.关闭socket对象 s.close() conn.close()
"""接收客户端信息"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 8885)) # 链接地址ipv4 通讯类型TCP
s.listen()
print('服务器启动,循环监听中')
while True:
conn, address = s.accept()
print('有客户端-> {0} 请求链接'.format(address))
data = conn.recv(1024)
print('收到客户端信息-> {0}'.format(data.decode()))
conn.send('服务器已经收到你的信息'.encode())
conn.close()
"""接收客户端上载文件"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 8881))
s.listen(10)
print('服务器启动')
while True: # 循环监听
with s.accept()[0] as conn:
buffer = []
while True: # 反复接收数据
data = conn.recv(1024)
if data:
buffer.append(data)
else:
break
b = bytes().join(buffer) # 缓冲区连接完整字节序列
with open(file='tcp.jpg', mode='wb') as f:
f.write(b)
print('数据上载完成')
客户端
import socket
# 1.创建socket对象
# 2.链接服务器地址、端口号 s.connect
# 3.发送信息给数据库。要转字节序列发送
# 4.接收服务器信息。要转解码转字符串
# 5.关闭socket
"""发送信息到服务器"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 8885))
s.send('请求进入系统'.encode())
data = s.recv(1024)
print('收到服务器信息-> {0}'.format(data.decode()))
s.close()
"""上传文件到服务器"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(('127.0.0.1', 8881))
with open(file='fj.jpg', mode='rb') as f:
b = f.read()
s.sendall(b)
print('数据上载完成')
- UDP
- HTTP 网址请求
import urllib.request
import urllib.parse
"""url网址请求"""
http = 'http://www.163.com'
with urllib.request.urlopen(http) as response:
data = response.read()
html = data.decode()
print(html)
try:
with open(file='website.html', mode='w', encoding='utf-8') as f:
aft = f.write(html)
print('文件保存成功,写入{0}字节'.format(aft))
except OSError as e:
print(e)
"""get请求"""
http = 'https://www.163.com/news'
params_dict = {'page': 3}
params_str = urllib.parse.urlencode(params_dict)
url = http + '?' + params_str
print(url)
req = urllib.request.Request(url)
with urllib.request.urlopen(req) as response:
data = response.read()
html = data.decode()
print(html)
try:
with open(file='website.html', mode='w+', encoding='utf-8') as f:
aft = f.write(html)
print('写入{0}字节'.format(aft))
except OSError as e:
print(e)
"""请求网址步骤"""
# 1. 定义网址变量 url
# 2. 定义传参变量 parma_dict
# 3. 转传参变量为url字符串 parma_str
# 4. 转url字符串为字节序列 param_byte
# 5. 创建Request对象
# 6. 打开请求网址
# 7. 读取数据,数据是字节序列
# 8. 转字节序列为字符串
"""post请求"""
url = 'https://www.163.com/news'
params_dict = {'page': 2}
params_str = urllib.parse.urlencode(params_dict)
# post不需要拼接字符串,POST是字节序列形式发送
params_byte = params_str.encode()
req = urllib.request.Request(url, data=params_byte)
with urllib.request.urlopen(req) as response:
data = response.read()
html = data.decode()
try:
with open(file='website.html', mode='w', encoding='utf-8') as f:
aft = f.write(html)
print('写入%s字节成功' % aft)
except OSError as e:
print('e')
# 下载图片
url = 'http://www.paris-girafe.com/Uploads/201606/57689efc7316e.jpg'
with urllib.request.urlopen(url) as response:
data = response.read()
try:
with open(file='fj.jpg', mode='wb') as f:
f.write(data)
except OSError as e:
print(e)