top of page

THE CODE

Some templates to get you started

THE BASE TEMPLATE

Ok, it looks a little scary, but you only need to worry about 2 or 3 lines. The rest you just cut'n'paste and forget. Which lines, line 6 which needs to be the address of your server and line 27/28 where you going to describe the actions to take when see this input. In this example they press key 5 and the server stops & the script ends. Note this script is designed to run in a terminal window of an Apple computer.

  1. #!/usr/bin/python

  2. # use export IPADDR=`ipconfig getifaddr en0` under OSX

  3. import random

  4. import os


  5. hostAddress = "1.1.1.1"

  6. port = random.randint(50000,50999)


  7. print("host ",hostAddress)

  8. print("port",port)


  9. online = True


  10. import socket

  11. backlog = 1

  12. size = 1024

  13. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  14. s.bind((hostAddress,port))

  15. s.listen(backlog)


  16. try:

  17.     client, address = s.accept()

  18.     while online:

  19.         data = client.recv(size)

  20.         if data:

  21.             print('>',data,'<')

  22.             if data == "=:5S":

  23.                 online = False

  24.         

  25. except:    

  26.     print("Closing socket")    

  27.     client.close()

  28.     s.close()

  29.     exit()

FEEDBACK TEMPLATE

This is almost the same as the previous one, with the exception of the definition of method portFeed to send data from the terminal back to remote. In this case just random number. You need to customise what gets sent back lines 14-24 and line 49 to 52. All the rest, no need to touch!

  1. #!/usr/bin/python

  2. import random

  3. from time import sleep

  4. from threading import Thread

  5. import random

  6. import os

  7. hostAddress = "1.1.1.1"

  8. port = random.randint(50000,50999)

  9. online = True

  10. def portFeed():

  11.     global online

  12.     while online:

  13.             # MUST feed strings ONLY

  14.             # all strings MUST be teminated with a return

  15.         try:

  16.                 rand = random.randint(1,10)

  17.                 feedme = "1P:" + str(rand) + "\n"

  18.                 data2 = bytes(feedme)

  19.                 client.send(data2)

  20.             sleep(2)

  21.             except:

  22.                 online = False

  23.         client.close()

  24.         exit()

  25.                 

  26. t1 = Thread(target=portFeed)

  27. print("host",hostAddress)

  28. print("port",port)

  29. import socket

  30. backlog = 1

  31. size = 1024

  32. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  33. s.bind((hostAddress,port))

  34. s.listen(backlog)

  35. try:

  36.     client, address = s.accept()

  37.     while online:

  38.         data = client.recv(size)

  39.         if data:

  40.             print('>',data,'<')

  41.             if t1.is_alive() is False:

  42.                 t1.start()

  43.             if data == "=:5S":

  44.                 online = False

  45. except:    

  46.     print("Closing socket")    

  47.     client.close()

  48.     s.close()

  49.     exit()

THE CUSTOM TEMPLATE

Same as the one above, your just do incremental changes. This time you added a methods to customise the port names and the buttons, line 18-39. And you add a little more code to the run loop at the end 70-76 to execute it.

  1. #!/usr/bin/python

  2. from time import sleep

  3. from threading import Thread

  4.  

  5. import socket

  6. import random

  7. import os

  8.  

  9. hostAddress = '1.1.1.1'

  10. port = random.randint(50000,50999)

  11.  

  12. print("host ",hostAddress)

  13. print("port",port)

  14.  

  15. transmit = False

  16. online = True

  17.  

  18. def configure():

  19.     try:

  20.         # configure labels on buttons/ports

  21.         ports = ["title:AP:alpha\n","title:BP:zeta\n","title:CP:delta\n","title:DP:beta\n","title:1P:omega\n","title:2P:gamma\n"]

  22.         for port in ports:

  23.             print("port",port)

  24.             data = bytes(port)

  25.             client.send(data)

  26.             sleep(0.5)

  27.         buttons = ["title:2S:forward\n","title:8S:back\n","title:4S:left\n","title:6S:right\n"]

  28.         for button in buttons:

  29.             print("button",button)

  30.             data = bytes(button)

  31.             client.send(data)

  32.             sleep(0.5)

  33.         return(True)

  34.     except AssertionError as error:

  35.         print("Closing config socket",error)    

  36.         client.close()

  37.         s.close()

  38.     online = False

  39.         exit()

  40. def portFeed():

  41.     # MUST feed strings ONLY

  42.     while 1:

  43.     try:

  44.         rand = random.randint(50000,50999)

  45.         portFeed = "PD:" + str(rand) + "\n"

  46.         dataD = bytes(portFeed)

  47.         client.send(dataD)

  48.         sleep(1)

  49.         portFeed = ""

  50.     except:

  51.         online = False

  52.         client.close()

  53.         exit()

  54. tD = Thread(target=portFeed)

  55. backlog = 1

  56. size = 1024

  57. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  58. s.bind((hostAddress,port))

  59. s.listen(backlog)

  60. try:

  61.     client, address = s.accept()

  62.     while online:

  63.         data = client.recv(size)

  64.         print('>',data,'<')

  65.         if data == "=:5S":

  66.             print('renaming')

  67.             configure()

  68.             transmit = True

  69.         if transmit is True:

  70.             if tD.is_alive() is False and transmit is True:

  71.                 tD.start()

  72. except AssertionError as error:    

  73.     print("Closing socket",error)    

  74.     client.close()

  75.     s.close()

  1. #!/usr/bin/python

  2. from time import sleep

  3. from threading import Thread

  4.  

  5. import socket

  6. import random

  7. import os

  8.  

  9. hostAddress = '1.1.1.1'

  10. port = random.randint(50000,50999)

  11.  

  12. print("host ",hostAddress)

  13. print("port",port)

  14.  

  15. transmit = False

  16. online = True

  17.  

  18. def configure():

  19.     try:

  20.         # configure labels on buttons/ports

  21.         ports = ["title:AP:alpha\n","title:BP:zeta\n","title:CP:delta\n","title:DP:beta\n","title:1P:omega\n","title:2P:gamma\n"]

  22.         for port in ports:

  23.             print("port",port)

  24.             data = bytes(port)

  25.             client.send(data)

  26.             sleep(0.5)

  27.         buttons = ["title:2S:forward\n","title:8S:back\n","title:4S:left\n","title:6S:right\n"]

  28.         for button in buttons:

  29.             print("button",button)

  30.             data = bytes(button)

  31.             client.send(data)

  32.             sleep(0.5)

  33.         return(True)

  34.     except AssertionError as error:

  35.         print("Closing config socket",error)    

  36.         client.close()

  37.         s.close()

  38.     online = False

  39.         exit()

  40. def portFeed():

  41.     # MUST feed strings ONLY

  42.     while 1:

  43.     try:

  44.         rand = random.randint(50000,50999)

  45.         portFeed = "PD:" + str(rand) + "\n"

  46.         dataD = bytes(portFeed)

  47.         client.send(dataD)

  48.         sleep(1)

  49.         portFeed = ""

  50.     except:

  51.         online = False

  52.         client.close()

  53.         exit()

  54. tD = Thread(target=portFeed)

  55. backlog = 1

  56. size = 1024

  57. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  58. s.bind((hostAddress,port))

  59. s.listen(backlog)

  60. try:

  61.     client, address = s.accept()

  62.     while online:

  63.         data = client.recv(size)

  64.         print('>',data,'<')

  65.         if data == "=:5S":

  66.             print('renaming')

  67.             configure()

  68.             transmit = True

  69.         if transmit is True:

  70.             if tD.is_alive() is False and transmit is True:

  71.                 tD.start()

  72. except AssertionError as error:    

  73.     print("Closing socket",error)    

  74.     client.close()

  75.     s.close()

A VIDEO LINK

This a video of a template I running in the terminal window. It is the last one you see here in fact.

THE CODE

Some templates to get you started

THE BASE TEMPLATE

Ok, it looks a little scary, but you only need to worry about 2 or 3 lines. The rest you just cut'n'paste and forget. Which lines, line 6 which needs to be the address of your server and line 27/28 where you going to describe the actions to take when see this input. In this example they press key 5 and the server stops & the script ends. Note this script is designed to run in a terminal window of an Apple computer.

  1. #!/usr/bin/python

  2. # use export IPADDR=`ipconfig getifaddr en0` under OSX

  3. import random

  4. import os

  5.  

  6. hostAddress = "1.1.1.1"

  7. port = random.randint(50000,50999)

  8.  

  9. print("host ",hostAddress)

  10. print("port",port)

  11.  

  12. online = True

  13.  

  14. import socket

  15. backlog = 1

  16. size = 1024

  17. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  18. s.bind((hostAddress,port))

  19. s.listen(backlog)

  20.  

  21. try:

  22.     client, address = s.accept()

  23.     while online:

  24.         data = client.recv(size)

  25.         if data:

  26.             print('>',data,'<')

  27.             if data == "=:5S":

  28.                 online = False

  29.         

  30. except:    

  31.     print("Closing socket")    

  32.     client.close()

  33.     s.close()

  34.     exit()

FEEDBACK TEMPLATE

This is almost the same as the previous one, with the exception of the definition of method portFeed to send data from the terminal back to remote. In this case just random number. You need to customise what gets sent back lines 14-24 and line 49 to 52. All the rest, no need to touch!

  1. #!/usr/bin/python

  2. import random

  3. from time import sleep

  4. from threading import Thread

  5. import random

  6. import os

  7. hostAddress = "1.1.1.1"

  8. port = random.randint(50000,50999)

  9. online = True

  10. def portFeed():

  11.     global online

  12.     while online:

  13.             # MUST feed strings ONLY

  14.             # all strings MUST be teminated with a return

  15.         try:

  16.                 rand = random.randint(1,10)

  17.                 feedme = "1P:" + str(rand) + "\n"

  18.                 data2 = bytes(feedme)

  19.                 client.send(data2)

  20.             sleep(2)

  21.             except:

  22.                 online = False

  23.         client.close()

  24.         exit()

  25.                 

  26. t1 = Thread(target=portFeed)

  27. print("host",hostAddress)

  28. print("port",port)

  29. import socket

  30. backlog = 1

  31. size = 1024

  32. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  33. s.bind((hostAddress,port))

  34. s.listen(backlog)

  35. try:

  36.     client, address = s.accept()

  37.     while online:

  38.         data = client.recv(size)

  39.         if data:

  40.             print('>',data,'<')

  41.             if t1.is_alive() is False:

  42.                 t1.start()

  43.             if data == "=:5S":

  44.                 online = False

  45. except:    

  46.     print("Closing socket")    

  47.     client.close()

  48.     s.close()

  49.     exit()

THE CUSTOM TEMPLATE

Same as the one above, your just do incremental changes. This time you added a methods to customise the port names and the buttons, line 18-39. And you add a little more code to the run loop at the end 70-76 to execute it.

  1. #!/usr/bin/python

  2. from time import sleep

  3. from threading import Thread

  4.  

  5. import socket

  6. import random

  7. import os

  8.  

  9. hostAddress = '1.1.1.1'

  10. port = random.randint(50000,50999)

  11.  

  12. print("host ",hostAddress)

  13. print("port",port)

  14.  

  15. transmit = False

  16. online = True

  17.  

  18. def configure():

  19.     try:

  20.         # configure labels on buttons/ports

  21.         ports = ["title:AP:alpha\n","title:BP:zeta\n","title:CP:delta\n","title:DP:beta\n","title:1P:omega\n","title:2P:gamma\n"]

  22.         for port in ports:

  23.             print("port",port)

  24.             data = bytes(port)

  25.             client.send(data)

  26.             sleep(0.5)

  27.         buttons = ["title:2S:forward\n","title:8S:back\n","title:4S:left\n","title:6S:right\n"]

  28.         for button in buttons:

  29.             print("button",button)

  30.             data = bytes(button)

  31.             client.send(data)

  32.             sleep(0.5)

  33.         return(True)

  34.     except AssertionError as error:

  35.         print("Closing config socket",error)    

  36.         client.close()

  37.         s.close()

  38.     online = False

  39.         exit()

  40. def portFeed():

  41.     # MUST feed strings ONLY

  42.     while 1:

  43.     try:

  44.         rand = random.randint(50000,50999)

  45.         portFeed = "PD:" + str(rand) + "\n"

  46.         dataD = bytes(portFeed)

  47.         client.send(dataD)

  48.         sleep(1)

  49.         portFeed = ""

  50.     except:

  51.         online = False

  52.         client.close()

  53.         exit()

  54. tD = Thread(target=portFeed)

  55. backlog = 1

  56. size = 1024

  57. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  58. s.bind((hostAddress,port))

  59. s.listen(backlog)

  60. try:

  61.     client, address = s.accept()

  62.     while online:

  63.         data = client.recv(size)

  64.         print('>',data,'<')

  65.         if data == "=:5S":

  66.             print('renaming')

  67.             configure()

  68.             transmit = True

  69.         if transmit is True:

  70.             if tD.is_alive() is False and transmit is True:

  71.                 tD.start()

  72. except AssertionError as error:    

  73.     print("Closing socket",error)    

  74.     client.close()

  75.     s.close()

A VIDEO LINK

This a video of a template I running in the terminal window. It is the last one you see here in fact.

bottom of page