网络编程(基于TCP协议)
基础(1)
文件名: myprofile.py
iaap = ("127.0.0.1", 8002)
文件名: server.py
"""
CS架构(client\server)最基本知识
"""
import socket
from myprofile_1 import iaap
# 1.创建socket对象(下面两个参数不写默认也是它俩)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # socket.SOCK_STREAM 流式协议
# 2.绑定地址
server.bind(iaap)
# 3.监听连接请求
server.listen(5) # 5 --> 半连接池大小
# 4.取出连接请求,开始服务
conn, addr = server.accept()
# 5.数据传输
date = conn.recv(1024)
date = date.decode("utf-8")
print(date)
# 6.结束服务
conn.close()
# 7.关闭服务器(可选)
server.close()
文件名: client.py
"""
CS架构(client\server)最基本知识
"""
import socket
from myprofile_1 import iaap
# 1.创建socket对象
client = socket.socket()
# 2.建立连接
client.connect(iaap)
# 3.传输数据
msg = input("请输入>>>")
client.send(msg.encode("utf-8"))
# 4.关闭连接(客户端的最后必须是关闭连接,从而释放客户端设备的资源)
client.close() # 所以必须要写,这行代码对应的是服务端的conn.close()
基础(2)
文件名: myprofile.py
iaap = ("127.0.0.1", 8004)
文件名: server.py
"""
CS架构升级--连接循环
"""
import socket
import time
from myprofile_2 import iaap
sk = socket.socket()
sk.bind(iaap)
sk.listen(5)
print("正在等待客户端连接...")
while True:
conn, addr = sk.accept()
print("成功连接客户端")
while True:
try:
date = conn.recv(1024)
except:
break
if not date:
break
conn.send(date.upper())
print("正在关闭连接...")
time.sleep(1.1)
conn.close()
print("连接已关闭")
文件名: client.py
"""
CS架构升级--连接循环
"""
import socket
from myprofile_2 import iaap
sk = socket.socket()
sk.connect(iaap)
while True:
msg = input("请输入>>>").strip()
if not msg:
continue
if msg == "Q" or msg == "q":
break
sk.send(msg.encode("utf-8"))
date = sk.recv(1024)
print(date.decode("utf-8"))
sk.close()
并发(1)
文件名: myprofile.py
iaap = ("127.0.0.1", 8010)
文件名: server.py
"""
利用 socketserver 实现并发
"""
import socketserver
from myprofile_1 import iaap
class RequestHandle(socketserver.BaseRequestHandler):
def handle(self):
print(self.request)
print(self.client_address)
while True:
try:
date = self.request.recv(1024)
except:
break
if not date:
break
self.request.send(date.upper())
print()
self.request.close()
sk = socketserver.ThreadingTCPServer(iaap, RequestHandle)
sk.serve_forever()
文件名: client.py
"""
利用 socketserver 实现并发
"""
import socket
from myprofile_1 import iaap
sk = socket.socket()
sk.connect(iaap)
while True:
msg = input("请输入>>>").strip()
if not msg:
continue
if msg == "Q" or msg == "q":
break
sk.send(msg.encode("utf-8"))
date = sk.recv(1024)
print(date.decode("utf-8"))
sk.close()
并发(2)
文件名: myprofile.py
iaap = ("127.0.0.1", 8033)
文件名: server.py
"""使用"进程"实现并发效果"""
import socket
from multiprocessing import Process
from myprofile_2 import iaap
def task(conn, addr):
ip, port = addr
print(f"成功连接客户端>>>\nIP>>>{ip}\n端口>>>{port}")
while True:
try:
date = conn.recv(1024)
except:
break
if not date:
break
conn.send(date.upper())
conn.close()
if __name__ == '__main__':
sk = socket.socket()
sk.bind(iaap)
sk.listen(5)
while True:
conn, addr = sk.accept()
p = Process(target=task, args=(conn, addr))
p.start()
文件名: client.py
"""使用"进程"实现并发效果"""
import socket
from myprofile_2 import iaap
sk = socket.socket()
sk.connect(iaap)
while True:
msg = input("请输入>>>").strip()
if not msg:
break
if msg == "q" or msg == "Q":
break
sk.send(msg.encode("utf-8"))
date = sk.recv(1024)
print(date.decode("utf-8"))
sk.close()
并发(3)
文件名: myprofile.py
iaap = ("127.0.0.1", 8001)
文件名: server.py
"""
使用线程实现并发效果
"""
import socket
from threading import Thread
from myprofile import iaap
def task(conn, addr):
ip, port = addr
print(f"客户端>>>IP:{ip}<<< >>>端口:{port}<<<\n连接成功\n")
while True:
try:
date = conn.recv(1024)
except:
break
if not date:
break
conn.send(date.upper())
print(f"客户端>>>IP:{ip}<<< >>>端口:{port}<<<正在关闭连接...")
conn.close()
print("连接已断开")
if __name__ == '__main__':
sk = socket.socket()
sk.bind(iaap)
sk.listen(5)
while True:
coon, addr = sk.accept()
t = Thread(target=task, args=(coon, addr))
t.start()
文件名: client.py
"""
使用线程实现并发效果
"""
import socket
from myprofile import iaap
sk = socket.socket()
sk.connect(iaap)
while True:
msg = input("请输入>>>").strip()
if not msg:
continue
if msg == "Q" or msg == "q":
break
sk.send(msg.encode("utf-8"))
date = sk.recv(1024)
print(date.decode("utf-8"))
print("正在关闭连接...")
sk.close()
print("连接已断开")
网络编程(基于UDP协议)
基础
文件名: myprofile
iaap = ("127.0.0.1", 8002)
文件名: server
"""
udp协议下的套接字
udp特点:
1.udp传输不可靠
2.udp不需要连接
"""
import socket
from myprofile import iaap
sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sk.bind(iaap)
while True:
date, addr = sk.recvfrom(1024)
sk.sendto(date.upper(), addr)
文件名: client
"""
udp协议下的套接字
udp特点:
1.udp传输不可靠
2.udp不需要连接
"""
import socket
from myprofile import iaap
sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
msg = input("请输入>>>").strip()
if not msg:
continue
if msg == "q" or msg == "Q":
break
sk.sendto(msg.encode("utf-8"), iaap)
date, addr = sk.recvfrom(1024)
print(date.decode("utf-8"))
print("正在断开连接...")
sk.close()
print("已断开")
并发
文件名: myprofile
iaap = ("127.0.0.1", 8000)
文件名: server
"""
利用 socketserver 实现并发
"""
import socketserver
import time
from myprofile import iaap
class RequestHandle(socketserver.BaseRequestHandler):
def handle(self):
print(f"客户端发来的数据>>>{self.request[0]}")
time.sleep(3)
self.request[1].sendto(self.request[0].upper(), self.client_address)
sk = socketserver.ThreadingUDPServer(iaap, RequestHandle)
sk.serve_forever()
文件名: client
"""
利用 socketserver 实现并发
"""
import socket
from myprofile import iaap
sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
msg = input("请输入>>>").strip()
if not msg:
continue
if msg == "q" or msg == "Q":
break
sk.sendto(msg.encode("utf-8"), iaap)
date, addr = sk.recvfrom(1024)
print(date.decode("utf-8"))
sk.close()