Python sctp模块服务器sid

2024-04-28 20:18:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试测试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()

有人有幸在服务器端使用pythonsctp模块吗? 提前谢谢你!在


Tags: 代码网络服务器client客户端客户机部署def
1条回答
网友
1楼 · 发布于 2024-04-28 20:18:35

我知道这个话题有点过时,但我想无论如何我都会回应它,以帮助社区。在

简而言之:

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

这里有一些代码可以帮助您入门,虽然有点冗长,但它演示了完整的连接,包括发送、接收和关闭连接。在

您可以在您的dev计算机上运行它,然后使用ncatnmap的{})这样的工具进行连接,即: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() 

相关问题 更多 >