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.
#!/usr/bin/python
# use export IPADDR=`ipconfig getifaddr en0` under OSX
import random
import os
hostAddress = "1.1.1.1"
port = random.randint(50000,50999)
print("host ",hostAddress)
print("port",port)
online = True
import socket
backlog = 1
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((hostAddress,port))
s.listen(backlog)
try:
client, address = s.accept()
while online:
data = client.recv(size)
if data:
print('>',data,'<')
if data == "=:5S":
online = False
except:
print("Closing socket")
client.close()
s.close()
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!
#!/usr/bin/python
import random
from time import sleep
from threading import Thread
​
import random
import os
​
hostAddress = "1.1.1.1"
port = random.randint(50000,50999)
​
online = True
​
def portFeed():
global online
while online:
# MUST feed strings ONLY
# all strings MUST be teminated with a return
try:
rand = random.randint(1,10)
feedme = "1P:" + str(rand) + "\n"
data2 = bytes(feedme)
client.send(data2)
sleep(2)
except:
online = False
client.close()
exit()
​
t1 = Thread(target=portFeed)
​
print("host",hostAddress)
print("port",port)
​
import socket
backlog = 1
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((hostAddress,port))
s.listen(backlog)
​
try:
client, address = s.accept()
while online:
data = client.recv(size)
if data:
print('>',data,'<')
if t1.is_alive() is False:
t1.start()
if data == "=:5S":
online = False
except:
print("Closing socket")
client.close()
s.close()
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.
-
#!/usr/bin/python
-
from time import sleep
-
from threading import Thread
-
-
import socket
-
import random
-
import os
-
-
hostAddress = '1.1.1.1'
-
port = random.randint(50000,50999)
-
-
print("host ",hostAddress)
-
print("port",port)
-
-
transmit = False
-
online = True
-
-
def configure():
-
try:
-
# configure labels on buttons/ports
-
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"]
-
for port in ports:
-
print("port",port)
-
data = bytes(port)
-
client.send(data)
-
sleep(0.5)
-
buttons = ["title:2S:forward\n","title:8S:back\n","title:4S:left\n","title:6S:right\n"]
-
for button in buttons:
-
print("button",button)
-
data = bytes(button)
-
client.send(data)
-
sleep(0.5)
-
return(True)
-
except AssertionError as error:
-
print("Closing config socket",error)
-
client.close()
-
s.close()
-
online = False
-
exit()
-
​
-
def portFeed():
-
# MUST feed strings ONLY
-
while 1:
-
try:
-
rand = random.randint(50000,50999)
-
portFeed = "PD:" + str(rand) + "\n"
-
dataD = bytes(portFeed)
-
client.send(dataD)
-
sleep(1)
-
portFeed = ""
-
except:
-
online = False
-
client.close()
-
exit()
-
​
-
​
-
tD = Thread(target=portFeed)
-
​
-
backlog = 1
-
size = 1024
-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
s.bind((hostAddress,port))
-
s.listen(backlog)
-
​
-
try:
-
client, address = s.accept()
-
while online:
-
data = client.recv(size)
-
print('>',data,'<')
-
if data == "=:5S":
-
print('renaming')
-
configure()
-
transmit = True
-
if transmit is True:
-
if tD.is_alive() is False and transmit is True:
-
tD.start()
-
​
-
except AssertionError as error:
-
print("Closing socket",error)
-
client.close()
-
s.close()
-
#!/usr/bin/python
-
from time import sleep
-
from threading import Thread
-
-
import socket
-
import random
-
import os
-
-
hostAddress = '1.1.1.1'
-
port = random.randint(50000,50999)
-
-
print("host ",hostAddress)
-
print("port",port)
-
-
transmit = False
-
online = True
-
-
def configure():
-
try:
-
# configure labels on buttons/ports
-
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"]
-
for port in ports:
-
print("port",port)
-
data = bytes(port)
-
client.send(data)
-
sleep(0.5)
-
buttons = ["title:2S:forward\n","title:8S:back\n","title:4S:left\n","title:6S:right\n"]
-
for button in buttons:
-
print("button",button)
-
data = bytes(button)
-
client.send(data)
-
sleep(0.5)
-
return(True)
-
except AssertionError as error:
-
print("Closing config socket",error)
-
client.close()
-
s.close()
-
online = False
-
exit()
-
​
-
def portFeed():
-
# MUST feed strings ONLY
-
while 1:
-
try:
-
rand = random.randint(50000,50999)
-
portFeed = "PD:" + str(rand) + "\n"
-
dataD = bytes(portFeed)
-
client.send(dataD)
-
sleep(1)
-
portFeed = ""
-
except:
-
online = False
-
client.close()
-
exit()
-
​
-
​
-
tD = Thread(target=portFeed)
-
​
-
backlog = 1
-
size = 1024
-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
s.bind((hostAddress,port))
-
s.listen(backlog)
-
​
-
try:
-
client, address = s.accept()
-
while online:
-
data = client.recv(size)
-
print('>',data,'<')
-
if data == "=:5S":
-
print('renaming')
-
configure()
-
transmit = True
-
if transmit is True:
-
if tD.is_alive() is False and transmit is True:
-
tD.start()
-
​
-
except AssertionError as error:
-
print("Closing socket",error)
-
client.close()
-
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.
-
#!/usr/bin/python
-
# use export IPADDR=`ipconfig getifaddr en0` under OSX
-
import random
-
import os
-
-
hostAddress = "1.1.1.1"
-
port = random.randint(50000,50999)
-
-
print("host ",hostAddress)
-
print("port",port)
-
-
online = True
-
-
import socket
-
backlog = 1
-
size = 1024
-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
s.bind((hostAddress,port))
-
s.listen(backlog)
-
-
try:
-
client, address = s.accept()
-
while online:
-
data = client.recv(size)
-
if data:
-
print('>',data,'<')
-
if data == "=:5S":
-
online = False
-
-
except:
-
print("Closing socket")
-
client.close()
-
s.close()
-
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!
#!/usr/bin/python
import random
from time import sleep
from threading import Thread
​
import random
import os
​
hostAddress = "1.1.1.1"
port = random.randint(50000,50999)
​
online = True
​
def portFeed():
global online
while online:
# MUST feed strings ONLY
# all strings MUST be teminated with a return
try:
rand = random.randint(1,10)
feedme = "1P:" + str(rand) + "\n"
data2 = bytes(feedme)
client.send(data2)
sleep(2)
except:
online = False
client.close()
exit()
​
t1 = Thread(target=portFeed)
​
print("host",hostAddress)
print("port",port)
​
import socket
backlog = 1
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((hostAddress,port))
s.listen(backlog)
​
try:
client, address = s.accept()
while online:
data = client.recv(size)
if data:
print('>',data,'<')
if t1.is_alive() is False:
t1.start()
if data == "=:5S":
online = False
except:
print("Closing socket")
client.close()
s.close()
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.
-
#!/usr/bin/python
-
from time import sleep
-
from threading import Thread
-
-
import socket
-
import random
-
import os
-
-
hostAddress = '1.1.1.1'
-
port = random.randint(50000,50999)
-
-
print("host ",hostAddress)
-
print("port",port)
-
-
transmit = False
-
online = True
-
-
def configure():
-
try:
-
# configure labels on buttons/ports
-
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"]
-
for port in ports:
-
print("port",port)
-
data = bytes(port)
-
client.send(data)
-
sleep(0.5)
-
buttons = ["title:2S:forward\n","title:8S:back\n","title:4S:left\n","title:6S:right\n"]
-
for button in buttons:
-
print("button",button)
-
data = bytes(button)
-
client.send(data)
-
sleep(0.5)
-
return(True)
-
except AssertionError as error:
-
print("Closing config socket",error)
-
client.close()
-
s.close()
-
online = False
-
exit()
-
​
-
def portFeed():
-
# MUST feed strings ONLY
-
while 1:
-
try:
-
rand = random.randint(50000,50999)
-
portFeed = "PD:" + str(rand) + "\n"
-
dataD = bytes(portFeed)
-
client.send(dataD)
-
sleep(1)
-
portFeed = ""
-
except:
-
online = False
-
client.close()
-
exit()
-
​
-
​
-
tD = Thread(target=portFeed)
-
​
-
backlog = 1
-
size = 1024
-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
s.bind((hostAddress,port))
-
s.listen(backlog)
-
​
-
try:
-
client, address = s.accept()
-
while online:
-
data = client.recv(size)
-
print('>',data,'<')
-
if data == "=:5S":
-
print('renaming')
-
configure()
-
transmit = True
-
if transmit is True:
-
if tD.is_alive() is False and transmit is True:
-
tD.start()
-
​
-
except AssertionError as error:
-
print("Closing socket",error)
-
client.close()
-
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.