#网络通信三要素 #IP地址 #应用程序端口#通信协议(TCP、UDP)
=======================不间断聊天和推出处理============================
import socket #参数有family,type #family=AF_INET, type=SOCK_STREAM #server端 sk = socket.socket() #创建socket对象 address = ('127.0.0.1', 8000) sk.bind(address) #绑定ip地址和端口 sk.listen(3) #允许同时3人连接 while True: conn, addr = sk.accept() while True: try: date = conn.recv(1024) except Exception: #防止client端硬性退出 break print(str(date, 'utf8')) if str(date, 'utf8') == 'exit': break inp = input('>>>>') conn.send(bytes(inp, 'utf8')) #发送接收的都是byte类型 conn.close() #print(conn) #(<socket.socket fd=208, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, # laddr=('127.0.0.1', 8000), raddr=('127.0.0.1', 50614)>, ('127.0.0.1', 50614))
import socket #client端 sk = socket.socket() address = ('127.0.0.1', 8000) #server端的ip地址和端口 sk.connect(address) while True: inp = input('>>>>') sk.send(bytes(inp, 'utf8')) if inp == 'exit': break data = sk.recv(1024) #一次接收多大的数据 print(str(data, 'utf8')) sk.close()=======================远程执行命令===========================
import socket import subprocess #cmd_server端 sk = socket.socket() address = ('127.0.0.1', 8000) sk.bind(address) sk.listen(2) while True: conn,addr = sk.accept() while True: try: data = conn.recv(1024) except Exception as e: break print(str(data, 'utf8')) if str(data, 'utf8') == 'exit': break obj = subprocess.Popen(str(data, 'utf8'), shell=True, stdout=subprocess.PIPE) cmd_reslt = obj.stdout.read() #执行命令行的结果 reskt_len = bytes(str(len(cmd_reslt)), 'utf8') conn.sendall(reskt_len) conn.sendall(cmd_reslt) conn.close()import socket #cmd_client端 sk = socket.socket() address = ('127.0.0.1', 8000) sk.connect(address) while True: message = input('>>>') sk.send(bytes(message, 'utf8')) if message == 'exit': break reslt_len = int(str(sk.recv(1024), 'utf8')) print(reslt_len) data = bytes() while len(data) != reslt_len: recv = sk.recv(1024) data += recv #print(data) #data = sk.recv(4024) print(str(data, 'gbk')) sk.close()========================================上传文件=============================import socket import os#server端 sk = socket.socket() address = ('127.0.0.1', 8000) sk.bind(address) sk.listen(2) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) while True: conn, addr = sk.accept() while True: data = conn.recv(1024) cmd, filename, file_size = str(data, 'utf8').split('|') path = os.path.join(BASE_DIR, 'yuan', filename) file_size = int(file_size) f = open(path, 'ab') has_recv = 0 while has_recv != file_size: data = conn.recv(1024) f.write(data) has_recv += len(data) f.close() conn.close()import socket import os#client端 sk = socket.socket() address = ('127.0.0.1', 8000) sk.connect(address) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) while True: masg = input('>>>') #post|11.png cmd, path = masg.split('|') #分割 path = os.path.join(BASE_DIR, path) #拼接 filename = os.path.basename(path) #文件名 file_size = os.stat(path).st_size#文件大小 file_info = 'post|%s|%s' % (filename, file_size) sk.send(bytes(file_info, 'utf8')) f = open(path, 'rb') has_sent = 0 while has_sent != file_size: data = f.read(1024) sk.sendall(data) has_sent += len(data) f.close() print('上传成功') sk.close()