Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
Jeopardy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Hacksaar
Jeopardy
Commits
b138e2f9
Commit
b138e2f9
authored
Apr 20, 2015
by
Agrigor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More OOP for client.py
parent
8bfb48cb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
58 deletions
+59
-58
client/client.py
client/client.py
+59
-58
No files found.
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
Markdown
is supported
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