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
b138e2f9
Commit
b138e2f9
authored
Apr 20, 2015
by
Agrigor
Browse files
More OOP for client.py
parent
8bfb48cb
Changes
1
Hide whitespace changes
Inline
Side-by-side
client/client.py
View file @
b138e2f9
###############################################################################
########################## IMPORT LIBRARIES ###################################
###############################################################################
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ IMPORT LIBRARIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import
time
import
signal
import
sys
import
socket
import
threading
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ GLOBAL CONSTANTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ CLASS DEFINITION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
###############################################################################
########################## GLOBAL CONSTANTS ###################################
###############################################################################
# Network Constants
SOURCE_IP
=
sys
.
argv
[
1
]
PORT
=
50000
UDP
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
###############################################################################
########################## DEFINITION FUNCTIONS ###############################
###############################################################################
def
init
():
# Initialize source ip adress
try
:
socket
.
inet_aton
(
SOURCE_IP
)
except
socket
.
error
:
print
(
"First argument was no valid IP-address"
)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MAIN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class
Client
:
def
__init__
(
self
,
ip
,
port
):
self
.
source_ip
=
ip
self
.
port
=
port
# Initialize source ip adress
try
:
socket
.
inet_aton
(
self
.
source_ip
)
except
socket
.
error
:
print
(
"First argument was no valid IP-address"
)
sys
.
exit
(
0
)
else
:
self
.
UDP
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
self
.
UDP
.
bind
((
""
,
self
.
port
))
def
cleanup
(
self
):
self
.
UDP
.
close
()
print
(
"
\n
Bye Bye"
)
sys
.
exit
(
0
)
else
:
# Initialize signal handling for interrupt by CTRL+C or kill command
signal
.
signal
(
signal
.
SIGINT
,
quit
)
# Initialize UDP socket binding
UDP
.
bind
((
""
,
PORT
))
# Things to be done before end of programm
def
quit
(
signal
,
frame
):
UDP
.
close
()
print
(
"
\n
Bye Bye"
)
sys
.
exit
(
0
)
###############################################################################
################################## MAIN #######################################
###############################################################################
# Mainfunction which includes the whole program
def
main
():
# Initialize
init
()
while
True
:
# Wait on button event and print some lazy messages
def
main
(
self
):
try
:
daten
,
addr
=
UDP
.
recvfrom
(
1024
)
if
addr
[
0
]
==
SOURCE_IP
:
print
(
addr
[
0
],
daten
.
decode
())
self
.
daten
,
self
.
addr
=
self
.
UDP
.
recvfrom
(
1024
)
if
self
.
addr
[
0
]
==
self
.
source_ip
:
print
(
self
.
addr
[
0
],
self
.
daten
.
decode
())
else
:
print
(
"Any hacker (IP: "
+
addr
[
0
]
+
") tries to kidding me, now I'll kill him!"
)
print
(
"Any hacker (IP: "
+
self
.
addr
[
0
]
+
") tries to kidding me, now I'll kill him!"
)
except
OSError
:
print
(
"...no connection..."
)
time
.
sleep
(
1
)
finally
:
self
.
cleanup
()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
###############################################################################
################################# PROGRAM #####################################
###############################################################################
# Start Client
main
()
client
=
Client
(
sys
.
argv
[
1
],
50000
)
client
.
main
()
#
##############################################################################
#
################################### END ######################################
#
##############################################################################
#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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