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. ​

  6. import random

  7. import os

  8. ​

  9. hostAddress = "1.1.1.1"

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

  11. ​

  12. online = True

  13. ​

  14. def portFeed():

  15.     global online

  16.     while online:

  17.             # MUST feed strings ONLY

  18.             # all strings MUST be teminated with a return

  19.         try:

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

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

  22.                 data2 = bytes(feedme)

  23.                 client.send(data2)

  24.             sleep(2)

  25.             except:

  26.                 online = False

  27.         client.close()

  28.         exit()

  29.                 

  30. ​

  31. t1 = Thread(target=portFeed)

  32. ​

  33. print("host",hostAddress)

  34. print("port",port)

  35. ​

  36. import socket

  37. backlog = 1

  38. size = 1024

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

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

  41. s.listen(backlog)

  42. ​

  43. try:

  44.     client, address = s.accept()

  45.     while online:

  46.         data = client.recv(size)

  47.         if data:

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

  49.             if t1.is_alive() is False:

  50.                 t1.start()

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

  52.                 online = False

  53. except:    

  54.     print("Closing socket")    

  55.     client.close()

  56.     s.close()

  57.     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. ​

  41. def portFeed():

  42.     # MUST feed strings ONLY

  43.     while 1:

  44.     try:

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

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

  47.         dataD = bytes(portFeed)

  48.         client.send(dataD)

  49.         sleep(1)

  50.         portFeed = ""

  51.     except:

  52.         online = False

  53.         client.close()

  54.         exit()

  55. ​

  56. ​

  57. tD = Thread(target=portFeed)

  58. ​

  59. backlog = 1

  60. size = 1024

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

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

  63. s.listen(backlog)

  64. ​

  65. try:

  66.     client, address = s.accept()

  67.     while online:

  68.         data = client.recv(size)

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

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

  71.             print('renaming')

  72.             configure()

  73.             transmit = True

  74.         if transmit is True:

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

  76.                 tD.start()

  77. ​

  78. except AssertionError as error:    

  79.     print("Closing socket",error)    

  80.     client.close()

  81.     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. ​

  41. def portFeed():

  42.     # MUST feed strings ONLY

  43.     while 1:

  44.     try:

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

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

  47.         dataD = bytes(portFeed)

  48.         client.send(dataD)

  49.         sleep(1)

  50.         portFeed = ""

  51.     except:

  52.         online = False

  53.         client.close()

  54.         exit()

  55. ​

  56. ​

  57. tD = Thread(target=portFeed)

  58. ​

  59. backlog = 1

  60. size = 1024

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

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

  63. s.listen(backlog)

  64. ​

  65. try:

  66.     client, address = s.accept()

  67.     while online:

  68.         data = client.recv(size)

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

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

  71.             print('renaming')

  72.             configure()

  73.             transmit = True

  74.         if transmit is True:

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

  76.                 tD.start()

  77. ​

  78. except AssertionError as error:    

  79.     print("Closing socket",error)    

  80.     client.close()

  81.     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. ​

  6. import random

  7. import os

  8. ​

  9. hostAddress = "1.1.1.1"

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

  11. ​

  12. online = True

  13. ​

  14. def portFeed():

  15.     global online

  16.     while online:

  17.             # MUST feed strings ONLY

  18.             # all strings MUST be teminated with a return

  19.         try:

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

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

  22.                 data2 = bytes(feedme)

  23.                 client.send(data2)

  24.             sleep(2)

  25.             except:

  26.                 online = False

  27.         client.close()

  28.         exit()

  29.                 

  30. ​

  31. t1 = Thread(target=portFeed)

  32. ​

  33. print("host",hostAddress)

  34. print("port",port)

  35. ​

  36. import socket

  37. backlog = 1

  38. size = 1024

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

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

  41. s.listen(backlog)

  42. ​

  43. try:

  44.     client, address = s.accept()

  45.     while online:

  46.         data = client.recv(size)

  47.         if data:

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

  49.             if t1.is_alive() is False:

  50.                 t1.start()

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

  52.                 online = False

  53. except:    

  54.     print("Closing socket")    

  55.     client.close()

  56.     s.close()

  57.     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. ​

  41. def portFeed():

  42.     # MUST feed strings ONLY

  43.     while 1:

  44.     try:

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

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

  47.         dataD = bytes(portFeed)

  48.         client.send(dataD)

  49.         sleep(1)

  50.         portFeed = ""

  51.     except:

  52.         online = False

  53.         client.close()

  54.         exit()

  55. ​

  56. ​

  57. tD = Thread(target=portFeed)

  58. ​

  59. backlog = 1

  60. size = 1024

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

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

  63. s.listen(backlog)

  64. ​

  65. try:

  66.     client, address = s.accept()

  67.     while online:

  68.         data = client.recv(size)

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

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

  71.             print('renaming')

  72.             configure()

  73.             transmit = True

  74.         if transmit is True:

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

  76.                 tD.start()

  77. ​

  78. except AssertionError as error:    

  79.     print("Closing socket",error)    

  80.     client.close()

  81.     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