标签:
网络编程之socket socket:在网络编程中的一个基本组件,也称套接字。 一个套接字就是socket模块中的socket类的一个实例。 套接字包括两个: 服务器套接字和客户机套接字 套接字的实例化需要3个参数: 1.地址簇:socket.AF_INET 2. 流:socket.SOCK_STREAM 3.使用的协议: 默认为0 服务器套接字:以下简称socket_server 客户端套接字:以下简称socket_client 地址:address=(‘127.0.0.1‘,8000) socket_server使用bind方法绑定address,再调用listen方法去监听,控制排队等候连接的最大数 socket_client使用connect方法连接到服务器,使用的地址与服务器bind的地址相同 套接字有两个方法: send(1024) #发送数据,每次最多发送1024bytes recv(1024) #接收数据,每次最多接收1024bytes **python库相关参考文档地址:http:python.org/doc/lib/module_socket.html *****简单的套接字交互***** server端: import socket # #family type sk=socket.socket() print(sk) address=(‘127.0.0.1‘,8000) sk.bind(address) sk.listen(3) print(‘waiting......‘) while 1: conn, addr = sk.accept() print(addr) while 1: try: data = conn.recv(1024) except Exception: break if not data: break print(‘.........‘, str(data, ‘utf8‘)) inp=input(‘>>>‘) conn.send(bytes(inp,‘utf8‘)) sk.close() client端: import socket sk=socket.socket() print(sk) address=(‘127.0.0.1‘,8000) sk.connect(address) while True: inp=input(‘>>>‘) if inp == ‘exit‘: break sk.send(bytes(inp,‘utf8‘)) data=sk.recv(1024) print(str(data,‘utf8‘)) sk.close() *****在client端输入shell命令,在服务器端执行命令后,返回结果***** server端: import subprocess import socket # #family type sk=socket.socket() print(sk) address=(‘127.0.0.1‘,8000) sk.bind(address) sk.listen(3) print(‘waiting......‘) while 1: conn, addr = sk.accept() print(addr) while 1: try: data = conn.recv(1024) except Exception: break if not data: break print(‘.........‘, str(data, ‘utf8‘)) obj=subprocess.Popen(data.decode(‘utf8‘),shell=True,stdout=subprocess.PIPE) cmd_result=obj.stdout.read() result_len=bytes(str(len(cmd_result)),‘utf8‘) print(‘>>>>>>‘,result_len) conn.sendall(result_len)#粘包现象 1600 conn.recv(1024)#解决粘包 conn.sendall(cmd_result) sk.close() client端: import subprocess import socket # #family type sk=socket.socket() print(sk) address=(‘127.0.0.1‘,8000) sk.bind(address) sk.listen(3) print(‘waiting......‘) while 1: conn, addr = sk.accept() print(addr) while 1: try: data = conn.recv(1024) except Exception: break if not data: break print(‘.........‘, str(data, ‘utf8‘)) obj=subprocess.Popen(data.decode(‘utf8‘),shell=True,stdout=subprocess.PIPE) cmd_result=obj.stdout.read() result_len=bytes(str(len(cmd_result)),‘utf8‘) print(‘>>>>>>‘,result_len) conn.sendall(result_len)#粘包现象 1600 conn.recv(1024)#解决粘包 conn.sendall(cmd_result) sk.close() *****ftp***** server端: import subprocess import socket # #family type import os sk=socket.socket() print(sk) address=(‘127.0.0.1‘,8000) sk.bind(address) sk.listen(3) print(‘waiting......‘) BASE_DIR=os.path.dirname(os.path.abspath(__file__)) while 1: conn, addr = sk.accept() while 1: data=conn.recv(1024) cmd,filename,filesize=str(data,‘utf8‘).split(‘|‘) path=os.path.join(BASE_DIR,‘yuan‘,filename) filesize=int(filesize) f=open(path,‘ab‘) has_receive=0 while has_receive!=filesize: data=conn.recv(1024) f.write(data) has_receive+=len(data) f.close() client端: import socket import os sk=socket.socket() print(sk) address=(‘127.0.0.1‘,8000) sk.connect(address) BASE_DIR=os.path.dirname(os.path.abspath(__file__)) while True: inp=input(‘>>>‘).strip()# post|11.png cmd,path=inp.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.sendall(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(‘上传成功‘)
标签:
原文地址:http://www.cnblogs.com/bk770466199/p/5912020.html