攻克python-socket
Posted 苦行僧PH
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了攻克python-socket相关的知识,希望对你有一定的参考价值。
socket
socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求。
sk.bind(address)
s.bind(address) 将套接字绑定到地址。address地址的格式取决于地址族。在AF_INET下,以元组(host,port)的形式表示地址。
sk.listen(backlog)
开始监听传入连接。backlog指定在拒绝连接之前,可以挂起的最大连接数量。
backlog等于5,表示内核已经接到了连接请求,但服务器还没有调用accept进行处理的连接个数最大为5
这个值不能无限大,因为要在内核中维护连接队列
sk.setblocking(bool)
是否阻塞(默认True),如果设置False,那么accept和recv时一旦无数据,则报错。
sk.accept()
接受连接并返回(conn,address),其中conn是新的套接字对象,可以用来接收和发送数据。address是连接客户端的地址。
接收TCP 客户的连接(阻塞式)等待连接的到来
sk.connect(address)
连接到address处的套接字。一般,address的格式为元组(hostname,port),如果连接出错,返回socket.error错误。
sk.connect_ex(address)
同上,只不过会有返回值,连接成功时返回 0 ,连接失败时候返回编码,例如:10061
sk.close()
关闭套接字
sk.recv(bufsize[,flag])
接受套接字的数据。数据以字符串形式返回,bufsize指定最多可以接收的数量。flag提供有关消息的其他信息,通常可以忽略。
sk.recvfrom(bufsize[.flag])
与recv()类似,但返回值是(data,address)。其中data是包含接收数据的字符串,address是发送数据的套接字地址。
sk.send(string[,flag])
将string中的数据发送到连接的套接字。返回值是要发送的字节数量,该数量可能小于string的字节大小。即:可能未将指定内容全部发送。
sk.sendall(string[,flag])
将string中的数据发送到连接的套接字,但在返回之前会尝试发送所有数据。成功返回None,失败则抛出异常。
内部通过递归调用send,将所有内容发送出去。
sk.sendto(string[,flag],address)
将数据发送到套接字,address是形式为(ipaddr,port)的元组,指定远程地址。返回值是发送的字节数。该函数主要用于UDP协议。
sk.settimeout(timeout)
设置套接字操作的超时期,timeout是一个浮点数,单位是秒。值为None表示没有超时期。一般,超时期应该在刚创建套接字时设置,因为它们可能用于连接的操作(如 client 连接最多等待5s )
sk.getpeername()
返回连接套接字的远程地址。返回值通常是元组(ipaddr,port)。
sk.getsockname()
返回套接字自己的地址。通常是一个元组(ipaddr,port)
sk.fileno()
套接字的文件描述符
import socket server = socket.socket() # 生成socket句柄 # server = socket.socket(AF.INET, sock_STREAM) server.bind((\'localhost\', 6960)) # 绑定监控的端口 server.listen(5) # 监听绑定的端口 print(\'waitting...\') conn, addr = server.accept() # 等待客户端连接 print(conn, addr) while True: data = conn.recv(1024) # 接收客户端数据 print(\'recv:\', data.decode()) conn.send(data) # 向客户端发送数据 conn.close()
import socket client = socket.socket() # 生成socket句柄 client.connect((\'localhost\', 9999)) # 链接服务器端 while True: msg = input(\'>>\').strip() if msg == 0: continue client.send(msg.encode(\'utf-8\')) # 向服务器端发送数据 data = client.recv(1024) print(\'recv:\', data.decode()) # 接收服务器端数据 client.close()
存在的问题
1.客户端断开时,服务器端也会自动断开报错。
解决方法:a.添加异常处理机制。
b.服务器端判断接受到的字符串是否为空(客户端断开后,会继续发送空字符)。
c.服务器使用socketserver。
2.可能存在的粘包问题(send函数发送数据,有一种机制会当发送的数据达到缓存区大小时或等待缓冲区超时,再统一发送,而不是send一次发送一次)。
解决方法:a.每发送一个文件,接受方收到后返回一个确实收到值,避免连续发送。
b.发送文件前,先将文件大小发送给接收方,使接受方只接收相应大小的数据。
实现简单的ssh
import socket,os #实例化socket ser=socket.socket() #绑定ip与端口 ser.bind((\'localhost\',9999)) #监听 ser.listen() while True: #阻塞,等待链接 print("开始等!") conn,addr=ser.accept() while True: #接受命令 try: data=conn.recv(1024) #执行命令返回字符 print("开始执行命令!") cmd_res=os.popen(data.decode()).read() if len(cmd_res)==0: cmd_res="命令错误" #发送字符长度 conn.send(str(len(cmd_res.encode("utf-8"))).encode("utf-8")) #接受回应,防止粘包 conn.recv(1024) #发送命令结果 conn.send(cmd_res.encode("utf-8")) print("发送完成!") except Exception as e: print(e) break
import socket #声明socket对象 cli=socket.socket() #链接 cli.connect(("localhost",9999)) while True: #输入命令 cmd=input(">>:") #传送命令 cli.send(cmd.encode("utf-8")) data_size=int(cli.recv(1024).decode()) cli.send(b"200") recv_data=b"" recv_size=0 #循环接受数据直到收完 while data_size>recv_size: #防止粘包 if data_size-recv_size<1024: size=data_size-recv_size else: size=1024 #接受数据 data=cli.recv(size) recv_size+=len(data) recv_data+=data else: print(recv_data.decode())
socketserver
为了实现服务器端多连接的问题,封装的socketserver模块。
The socketserver
module simplifies the task of writing network servers.
There are four basic concrete server classes:
- class
socketserver.
TCPServer
(server_address, RequestHandlerClass, bind_and_activate=True) -
This uses the Internet TCP protocol, which provides for continuous streams of data between the client and server. If bind_and_activate is true, the constructor automatically attempts to invoke
server_bind()
andserver_activate()
. The other parameters are passed to theBaseServer
base class.
- class
socketserver.
UDPServer
(server_address, RequestHandlerClass, bind_and_activate=True) -
This uses datagrams, which are discrete packets of information that may arrive out of order or be lost while in transit. The parameters are the same as for
TCPServer
.
- class
socketserver.
UnixStreamServer
(server_address, RequestHandlerClass, bind_and_activate=True) - class
socketserver.
UnixDatagramServer
(server_address, RequestHandlerClass,bind_and_activate=True) -
These more infrequently used classes are similar to the TCP and UDP classes, but use Unix domain sockets; they’re not available on non-Unix platforms. The parameters are the same as for
TCPServer
.
These four classes process requests synchronously; each request must be completed before the next request can be started. This isn’t suitable if each request takes a long time to complete, because it requires a lot of computation, or because it returns a lot of data which the client is slow to process. The solution is to create a separate process or thread to handle each request; the ForkingMixIn
and ThreadingMixIn
mix-in classes can be used to support asynchronous behaviour.
There are five classes in an inheritance diagram, four of which represent synchronous servers of four types:
+------------+
| BaseServer |
+------------+
|
v
+-----------+ +------------------+
| TCPServer |------->| UnixStreamServer |
+-----------+ +------------------+
|
v
+-----------+ +--------------------+
| UDPServer |------->| UnixDatagramServer |
+-----------+ +--------------------+
Note that UnixDatagramServer
derives from UDPServer
, not from UnixStreamServer
— the only difference between an IP and a Unix stream server is the address family, which is simply repeated in both Unix server classes.
- class
socketserver.
ForkingMixIn
- class
socketserver.
ThreadingMixIn
-
Forking and threading versions of each type of server can be created using these mix-in classes. For instance,
ThreadingUDPServer
is created as follows:class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
The mix-in class comes first, since it overrides a method defined in
UDPServer
. Setting the various attributes also changes the behavior of the underlying server mechanism.
- class
socketserver.
ForkingTCPServer
- class
socketserver.
ForkingUDPServer
- class
socketserver.
ThreadingTCPServer
- class
socketserver.
ThreadingUDPServer
-
These classes are pre-defined using the mix-in classes.
Request Handler Objects
class
socketserver.
BaseRequestHandler
This is the superclass of all request handler objects. It defines the interface, given below. A concrete request handler subclass must define a new
handle()
method, and can override any of the other methods. A new instance of the subclass is created for each request.setup
()-
Called before the
handle()
method to perform any initialization actions required. The default implementation does nothing.
handle
()-
This function must do all the work required to service a request. The default implementation does nothing. Several instance attributes are available to it; the request is available as
self.request
; the client address asself.client_address
; and the server instance asself.server
, in case it needs access to per-server information.The type of
self.request
is different for datagram or stream services. For stream services,self.request
is a socket object; for datagram services,self.request
is a pair of string and socket.
利用socketserver实现ssh客服端
import socketserver class MyTCPHandler(socketserver.BaseRequestHandler): def handle(self): while True: try: self.data = self.request.recv(1024).strip() print("{} wrote:".format(self.client_address[0])) print(self.data) self.request.send(self.data.upper()) except ConnectionResetError as e: print("err",e) break if __name__ == "__main__": HOST, PORT = "localhost", 9999 # Create the server, binding to localhost on port 9999 server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) server.serve_forever()
以上是关于攻克python-socket的主要内容,如果未能解决你的问题,请参考以下文章