首页 > 代码库 > 网络编程之socket

网络编程之socket

网络编程之socketsocket:在网络编程中的一个基本组件,也称套接字。一个套接字就是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 typesk=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:breakif not data: breakprint(........., str(data, utf8))inp=input(>>>)conn.send(bytes(inp,utf8))sk.close()client端:import socketsk=socket.socket()print(sk)address=(127.0.0.1,8000)sk.connect(address)while True:inp=input(>>>)if inp == exit:breaksk.send(bytes(inp,utf8))data=sk.recv(1024)print(str(data,utf8))sk.close()*****在client端输入shell命令,在服务器端执行命令后,返回结果*****server端:import subprocessimport socket# #family typesk=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:breakif not data: breakprint(........., 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)#粘包现象 1600conn.recv(1024)#解决粘包conn.sendall(cmd_result)sk.close()client端:import subprocessimport socket# #family typesk=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:breakif not data: breakprint(........., 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)#粘包现象 1600conn.recv(1024)#解决粘包conn.sendall(cmd_result)sk.close() *****ftp*****server端:import subprocessimport socket# #family typeimport ossk=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=0while has_receive!=filesize:data=conn.recv(1024)f.write(data)has_receive+=len(data)f.close()client端:import socketimport ossk=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.pngcmd,path=inp.split(|)path=os.path.join(BASE_DIR,path)filename=os.path.basename(path)file_size=os.stat(path).st_sizefile_info=post|%s|%s%(filename,file_size)sk.sendall(bytes(file_info,utf8))f=open(path,rb)has_sent=0while has_sent!=file_size:data=f.read(1024)sk.sendall(data)has_sent+=len(data)f.close()print(上传成功)

 

网络编程之socket