-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledcontrolserver.py
More file actions
36 lines (32 loc) · 853 Bytes
/
ledcontrolserver.py
File metadata and controls
36 lines (32 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## createbindcocket.py
import socket
import sys
## AF_INET = communicate on internet - always use this
## SOCK_STREAM = TCP connection - more reliable, use this not UDP
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
## Arbitrary port
try:
mysock.bind("", 1234)
except socket.error:
print("Failed to bind")
sys.exit()
## Listen and accept
## 5 is backlog, the number of requests allowed to wait for service
mysock.listen(5)
## liveserver.py
while True:
## Returns a connection (for sending/receiving) and an address (IP, port)
## conn = connection
## addr = address, ip + port
conn, addr = mysock.accept()
data = conn.recv(1000)
if not data:
break
## Switch the led on/off
## Data is a byte array, not string
if data == b'on':
GPIO.output(13, True)
if data == b'off':
GPIO.output(13, False)
conn.close()
mysock.close()