【问题标题】:Python sctp module - server sidePython sctp 模块 - 服务器端
【发布时间】:2014-10-27 00:47:45
【问题描述】:

我一直在尝试为网络部署测试 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 模块运气好? 提前谢谢你!

【问题讨论】:

  • 有什么问题?

标签: python debugging sctp


【解决方案1】:

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

简而言之:

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

这里有一些代码可以帮助您入门,它有点冗长,但它说明了一个完整的连接、发送、接收和关闭连接。

您可以在您的开发计算机上运行它,然后使用类似ncatnmapnetcat 实现)之类的工具进行连接,即: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() 

【讨论】:

    【解决方案2】:

    除非您需要特殊的 sctp_ 功能,否则您根本不需要 sctp 模块。 只需将协议 132 用作 IPPROTO_SCTP(在我的 python3 套接字模块上定义,但不在我的 python2 套接字模块上定义),您就可以使用标准套接字模块中的套接字、绑定、侦听、连接、发送、接收、发送到、接收从、关闭。

    我正在做一些 SCTP C 开发,我使用 python 来更好地理解没有 SCTP 模块的 SCTP 行为。

    【讨论】:

      猜你喜欢
      • 2023-02-14
      • 2017-07-02
      • 2016-04-09
      • 2020-10-08
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      相关资源
      最近更新 更多