Python sctp模块 - 服务器端

3 投票
2 回答
6390 浏览
提问于 2025-04-19 10:34

我一直在尝试测试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模块来做服务器端的?
提前谢谢大家!

2 个回答

0

如果你不需要那些特别的 sctp_ 函数,其实根本不需要 sctp 模块。你只要用协议 132,作为 IPPROTO_SCTP(在我的 Python3 套接字模块中有定义,但在我的 Python2 套接字模块中没有),这样你就可以使用标准套接字模块里的 socket、bind、listen、connect、send、recv、sendto、recvfrom 和 close 这些功能。

我正在做一些 SCTP 的 C 语言开发,为了更好地理解 SCTP 的行为,我用 Python 来进行测试,而不需要使用 SCTP 模块。

4

我知道这个话题有点过时了,但我还是想回应一下,帮助一下大家。

简单来说:

  • 你正在使用 pysctp 和 sockets 包来创建一个客户端或服务器;
  • 因此,你可以像平常一样用普通的 TCP 连接来创建你的服务器连接。

这里有一些代码可以帮助你入门,虽然有点啰嗦,但它展示了完整的连接过程,包括发送、接收和关闭连接。

你可以在你的开发电脑上运行这段代码,然后使用像 ncat 这样的工具(nmapnetcat 实现)来连接,比如: ncat --sctp localhost 80

不多说,这就是代码... 希望对你有帮助:

# 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() 

撰写回答