NOTICE: The Processors Wiki will End-of-Life on January 15, 2021. It is recommended to download any files or other content you may need that are hosted on processors.wiki.ti.com. The site is now set to read only.

Template:CC31xx & CC32xx Server Setup with Python Scripts

From Texas Instruments Wiki
Jump to: navigation, search

Using python scripts is an easy solution for peer server. The python package is open sourced and can be easily installed on any PC. This document will describe how to set the python environment on a PC and how to use the provided scripts.

Software requirements[edit]

  • Python 2.7.x - Please note that there is also python 3 installers on the web site, please stick to the python 2 versions, as the provided server scripts support of python 3 is not guaranteed. Install the package on your PC according to the instructions the installer prompts.
  • OpenSSL - In order to implement a secure server, OpenSSL must be installed. Please refer to CC31xx & CC32xx Generate Certificate for OpenSSL download and certificate generation. Place all certificate files under the same directory when you are done.

Peer server provided scripts[edit]

There are 2 python scripts provided in the packages:

  1. tcp_server.py – this script implements a TCP server.
  2. ssl_tcp_server.py – this script implements a TCP server with TLS/SLL.

Setting the connection parameters[edit]

Each one of the scripts requires the user to set the connection parameters the figures below show an example.

  • tcp_server.py setting example

<syntaxhighlight lang="python"> import socket,os

TCP_IP = '192.168.39.200' #<-- define the server IP address TCP_PORT = 5001 #<-- define the server listening port BUFFER_SIZE = 1400 #<-- define the buffer size idx = 1

  1. open TCP socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((TCP_IP, int(TCP_PORT))) print "waiting for RX" while 1:

   s.listen(1)
   conn, addr = s.accept()
   print 'Connection has been established with address: ', addr
   while 1:
       data = conn.recv(BUFFER_SIZE)
       # data will be null when the client closes the socket
       if not data: break 
       if data : print "received data:", idx
       #conn.sendall(data)  # echo if needed, uncomment this line if you want to reply
       idx = idx + 1
   conn.close()

</syntaxhighlight>


  • ssl_tcp_server.py setting example

<syntaxhighlight lang="python"> import socket, ssl

idx = 1 print "-- Server is starting --" bindsocket = socket.socket() bindsocket.bind((, 443)) print "-- Server is set and listening on port 443 --"

while True:

   print " Waiting for client requests ... "
   bindsocket.listen(1)
   newsocket, fromaddr = bindsocket.accept()
   connstream = ssl.wrap_socket(newsocket,
                                server_side=True,
                                certfile="cert.pem",
                                keyfile="cert_privkey.pem")
   while True:
       data =  connstream.read()
       if not data : break
       if data : print "packet number: ",idx
       idx += 1
   conststream.close()   

</syntaxhighlight>

Running the script[edit]

Open a command line terminal and run the command, just like in the figure 3 below. Make sure that the running computer is connected to same AP as your simplelink device.

  • Running Example

ssl_tcp_server example running