Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Hacksaar
Jeopardy
Commits
5dde531f
Commit
5dde531f
authored
Apr 21, 2015
by
Agrigor
Browse files
UDP communication is now in a class + some minor changes
parent
a4b62eee
Changes
2
Hide whitespace changes
Inline
Side-by-side
client/client.py
100644 → 100755
View file @
5dde531f
#!/usr/bin/python
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ IMPORT LIBRARIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
...
@@ -21,33 +23,28 @@ PORT = 50000
# ~~~~~~~~~~~~~~~~~~~~~~~~ CLASS DEFINITION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class
Co
nnec
tion
:
class
Co
munica
tion
:
def
__init__
(
self
,
ip
,
port
):
self
.
ip
=
ip
self
.
port
=
port
self
.
socket
=
""
self
.
__
ip
=
ip
self
.
__
port
=
port
self
.
__
socket
=
""
def
check_ip
(
self
):
try
:
socket
.
inet_aton
(
self
.
ip
)
socket
.
inet_aton
(
self
.
__
ip
)
return
True
except
socket
.
error
:
print
(
"First argument was no valid IP-address"
)
sys
.
exit
(
1
)
except
OSError
:
return
False
def
create_client_socket
(
self
):
self
.
socket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
self
.
socket
.
bind
((
""
,
self
.
port
))
self
.
__
socket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
self
.
__
socket
.
bind
((
""
,
self
.
__
port
))
def
receive
(
self
,
bytes
):
daten
,
addr
=
self
.
socket
.
recvfrom
(
bytes
)
if
addr
[
0
]
==
self
.
ip
:
print
(
addr
[
0
]
+
" -> "
+
daten
.
decode
())
else
:
print
(
"Any hacker (IP: "
+
addr
[
0
]
+
") tries to kidding me, now I'll kill him!"
)
return
self
.
__socket
.
recvfrom
(
bytes
)
def
cleanup
(
self
):
self
.
socket
.
close
()
self
.
__
socket
.
close
()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
...
@@ -57,17 +54,27 @@ class Connection:
class
Client
:
def
__init__
(
self
,
ip
,
port
):
self
.
foo
=
Connection
(
ip
,
port
)
if
self
.
foo
.
check_ip
():
self
.
foo
.
create_client_socket
()
self
.
UDP
=
Comunication
(
ip
,
port
)
self
.
IP
=
ip
if
self
.
UDP
.
check_ip
():
self
.
UDP
.
create_client_socket
()
else
:
print
(
"First argument was no valid IP-address"
)
sys
.
exit
(
1
)
def
main
(
self
):
try
:
self
.
foo
.
receive
(
1024
)
while
True
:
daten
,
addr
=
self
.
UDP
.
receive
(
1024
)
if
addr
[
0
]
==
self
.
IP
:
print
(
addr
[
0
]
+
" -> "
+
daten
.
decode
())
else
:
print
(
"Any hacker (IP: "
+
addr
[
0
]
+
") tries to kidding me, now I'll kill him!"
)
finally
:
self
.
cleanup
()
def
cleanup
(
self
):
self
.
UDP
.
cleanup
()
print
(
"
\n
Bye Bye"
)
sys
.
exit
(
0
)
...
...
server/server.py
View file @
5dde531f
#!/usr/bin/python3
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ IMPORT LIBRARIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
...
@@ -19,12 +21,13 @@ BOUNCE = 300
PORT
=
50000
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ CLASS DEFINITION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class
Co
nnec
tion
:
class
Co
mmunica
tion
:
def
__init__
(
self
,
ip
,
port
):
self
.
socket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
self
.
msg
=
""
...
...
@@ -44,7 +47,7 @@ class Connection:
def
notify
(
self
,
buzzer
):
self
.
msg
=
"Button "
+
str
(
buzzer
.
number
)
+
" pressed"
self
.
send
(
self
.
msg
)
print
(
self
.
msg
)
print
(
self
.
msg
+
" -> "
+
self
.
target_ip
)
class
Buzzer
:
...
...
@@ -75,9 +78,9 @@ class Buzzer:
class
Server
:
def
__init__
(
self
,
ip
,
port
):
self
.
connection
=
Connec
tion
(
ip
,
port
)
self
.
UDP
=
Communica
tion
(
ip
,
port
)
self
.
buzzers
=
[
Buzzer
(
i
,
pin
,
BOUNCE
)
for
i
,
pin
in
enumerate
(
BUZZER_PINS
,
start
=
1
)]
self
.
connection
.
observe_buzzers
(
self
.
buzzers
)
self
.
UDP
.
observe_buzzers
(
self
.
buzzers
)
# Initialize target ip address
try
:
socket
.
inet_aton
(
ip
)
...
...
@@ -87,7 +90,7 @@ class Server:
def
cleanup
(
self
):
GPIO
.
cleanup
()
self
.
connection
.
close
()
self
.
UDP
.
close
()
print
(
"
\n
Bye Bye"
)
sys
.
exit
(
0
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment