慕课网上《Python运维-Socket网络编程》课程个人总结笔记,侵删。
慕课网视频链接:https://www.imooc.com/learn/1031
一、课程介绍
1.1 socket简介
Socket是什么?
Socket是电脑网络中进程间数据流的端点
Socket是操作系统的通信机制
应用程序通过Socket进行网络数据的传输
Socket的通信方式?
Socket分为UDP和TCP两种不同的通信方式
为什么Socket?
Socket能够适应多种网络协议
Socket是基础应用,可举一反三
服务器传输大量涉及网络协议,离不开Socket
二、Socket入门通信
2.1 服务端程序
import socket
sk = socket.socket()
ip_port = ("127.0.0.1",8888)
sk.bind(ip_port)
sk.listen(5)
print ("waiting for receiving message")
conn,address = sk.accept()
msg = "hello world"
conn.send(msg.encode())
conn.close()
2.2 客户端程序
import socket
client = socket.socket()
ip_port = ("127.0.0.1",8888)
client.connect(ip_port)
data = client.recv(1024)
print (data.decode())
2.3 客户端连续消息发送
import socket
import random
sk = socket.socket()
ip_port = ("127.0.0.1",8888)
sk.bind(ip_port)
sk.listen(5)
while True:
print ("waiting for receiving message")
conn,address = sk.accept()
msg = "sucessfully connect"
conn.send(msg.encode())
while True:
data = conn.recv(1024)
print (data.decode())
if data == b'exit':
break
conn.send(data)
conn.send(str(random.randint(1,100)).encode())
conn.close()
import socket
client = socket.socket()
ip_port = ("127.0.0.1",8888)
client.connect(ip_port)
while True:
data = client.recv(1024)
print (data.decode())
msg_input = input("please enter the message:")
client.send(msg_input.encode())
if msg_input == "exit":
break
data = client.recv(1024)
print (data.decode())
2.4 实例化参数含义
family:地址簇
socket.AF_INET IPV4( 默认)
socket.AF_INET6 IPV6
socket.AF_UNIX 只能够用于单一的Unix系统进程间的通信
type:类型
socket.SOCK_STREAM 流式socket,for TCP(默认)
socket.SOCK_DGRAM 数据报式socket,for UDP
socket.SOCK_RAW 原始套接字
socket.SOCK_RDM 可靠UDP形式
socket.SOCK_SEQPACKET 可靠的连续数据包服务
proto:协议号
0:默认,可以省略
CAN_RAW或CAN_BCM:地址簇为AF_CAN时
三、网络通讯实现
3.1 udp通信
import socket
sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip_port = ("127.0.0.1",8888)
sk.bind(ip_port)
while True:
data = sk.recv(1024)
print (data.decode())
import socket
sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip_port = ("127.0.0.1",8888)
while True:
msg_input = input("please enter the message:")
if msg_input == "exit":
break
sk.sendto(msg_input.encode(),ip_port)
sk.close()
3.2-3.3 非阻塞模块
import socketserver
import random
class MyServer(socketserver.BaseRequestHandler):
def setup(self):
pass
def handle(self):
conn = self.request
msg = "hello world"
conn.send(msg.encode())
while True:
data = conn.recv(1024)
print (data.decode())
if data == b'exit':
break
conn.send(data)
conn.send(str(random.randint(1,100)).encode())
conn.close()
def finish(self):
pass
if __name__ == "__main__":
server = socketserver.ThreadingTCPServer(("127.0.0.1",8888),MyServer)
server.serve_forever()
import socket
client = socket.socket()
ip_port = ("127.0.0.1",8888)
client.connect(ip_port)
while True:
data = client.recv(1024)
print (data.decode())
msg_input = input("please enter the message:")
client.send(msg_input.encode())
if msg_input == "exit":
break
data = client.recv(1024)
print (data.decode())
四、文件上传下载
4.1 文件长传程序的编写
import socket
sk = socket.socket()
ip_port = ("127.0.0.1",9999)
sk.connect(ip_port)
with open('test.py','rb') as f:
for i in f:
sk.send(i)
sk.send('quit'.encode())
import socket
sk = socket.socket()
ip_port = ('127.0.0.1',9999)
sk.bind(ip_port)
sk.listen(5)
while True:
conn,address = sk.accept()
with open('file','ab') as f:
data = conn.recv(1024)
if data == b'quit':
break
f.write(data)
sk.close()
4.2 文件上传程序的拍错方法
import socket
sk = socket.socket()
ip_port = ("127.0.0.1",9999)
sk.connect(ip_port)
with open('test.py','rb') as f:
for i in f:
sk.send(i)
data = sk.recv(1024)
if data != b'success':
break
sk.send('quit'.encode())
import socket
sk = socket.socket()
ip_port = ('127.0.0.1',9999)
sk.bind(ip_port)
sk.listen(5)
while True:
conn,address = sk.accept()
while True:
with open('file','ab') as f:
data = conn.recv(1024)
if data == b'quit':
break
f.write(data)
conn.send('success'.encode())
print ("file recieved over!")
sk.close()