sidtg1
sidtg1

Reputation: 31

Python sctp module - server side

I have been trying to test SCTP for a network deployment. I do not have an SCTP server or client and was hoping to be able use pysctp.

I am fairly certain that I have the client side code working.

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

Has anybody had luck with using the python sctp module for the server side? Thank you in Advance!

Upvotes: 3

Views: 7008

Answers (2)

Marcelo Pacheco
Marcelo Pacheco

Reputation: 276

Unless you need the special sctp_ functions you don't need an sctp module at all. Just use protocol 132 as IPPROTO_SCTP (is defined on my python3 socket module but not on my python2 socket module) and you can use the socket,bind,listen,connect,send,recv,sendto,recvfrom,close from the standard socket module.

I'm doing some SCTP C development and I used python to better understand SCTP behavior without the SCTP module.

Upvotes: 0

Herby Drake
Herby Drake

Reputation: 41

I know that this topic's a bit dated, but I figured I would respond to it anyway to help out the community.

In a nutshell:

  • you are using pysctp with the sockets package to create either a client or a server;
  • you can therefore create your server connection as you normally would with a regular TCP connection.

Here's some code to get you started, it's a bit verbose, but it illustrates a full connection, sending, receiving, and closing the connection.

You can run it on your dev computer and then use a tool like ncat (nmap's implementation of netcat) to connect, i.e.: ncat --sctp localhost 80.

Without further ado, here's the code... 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() 

Upvotes: 4

Related Questions