Skip to main content
 首页 » 编程设计

Python sctp 模块之服务器端

2024年10月25日11cmt

我一直在尝试为网络部署测试 SCTP。 我没有 SCTP 服务器或客户端,希望能够使用 pysctp。

我相当确定我的客户端代码可以正常工作。

def sctp_client (): 
    print("SCTP client") 
    sock = sctp.sctpsocket_tcp(socket.AF_INET) 
    #sock.connect(('10.10.10.70',int(20003))) 
    sock.connect(('10.10.10.41',int(21000))) 
    print("Sending message") 
    sock.sctp_send(msg='allowed') 
    sock.shutdown(0) 
    sock.close() 

有没有人有幸在服务器端使用 python sctp 模块? 在此先感谢您!

请您参考如下方法:

我知道这个主题有点过时,但我想我还是会回复它以帮助社区。

简而言之:

  • 您正在使用 pysctp使用套接字包创建客户端或服务器;
  • 因此,您可以像通常使用常规 TCP 连接一样创建服务器连接。

这是一些入门代码,它有点冗长,但它说明了完整的连接、发送、接收和关闭连接。

您可以在您的开发计算机上运行它,然后使用类似 ncat 的工具( nmap 的 netcat 的实现)连接,即: ncat --sctp localhost 80 .

事不宜迟,这是代码...... HTH:

# Here are the packages that we need for our SCTP server                                         
import socket                                                                                    
import sctp                                                                                      
from   sctp import *                                                                             
import threading                                                                                 
 
# Let's create a socket:                                                                         
my_tcp_socket = sctpsocket_tcp(socket.AF_INET)                                                   
my_tcp_port   = 80                                                                               
 
# Here are a couple of parameters for the server                                                 
server_ip     = "0.0.0.0"                                                                        
backlog_conns = 3                                                                                
 
# Let's set up a connection:                                                                     
my_tcp_socket.events.clear()                                                                     
my_tcp_socket.bind((server_ip, my_tcp_port))                                                     
my_tcp_socket.listen(backlog_conns)                                                              
 
# Here's a method for handling a connection:                                                     
def handle_client(client_socket):                                                                
  client_socket.send("Howdy! What's your name?\n")                                               
  name = client_socket.recv(1024) # This might be a problem for someone with a reaaallly long name. 
  name = name.strip()                                                                            
 
  print "His name was Robert Paulson. Er, scratch that. It was {0}.".format(name)                
  client_socket.send("Thanks for calling, {0}. Bye, now.".format(name))                          
  client_socket.close()                                                                          
 
# Now, let's handle an actual connection:                                                        
while True:                                                                                      
    client, addr   = my_tcp_socket.accept()                                                      
    print "Call from {0}:{1}".format(addr[0], addr[1])                                           
 
    client_handler = threading.Thread(target = handle_client,                                    
                                      args   = (client,))                                        
    client_handler.start()