-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpClient.py
178 lines (155 loc) · 6.97 KB
/
udpClient.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''Project:
# # ## ## ## # ### ### ### ###
# # # # # # # # # # # # #
# # # # ## # # # ## # # #
# # # # # # # # # # # #
### ## # ## ### ### ### # # #
'''
import socket
import threading
from ipaddress import AddressValueError, IPv4Address
from os import getcwd
'''###################################################################'''
'''############# Defining practical & local functions: ###############'''
def find_iface_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
iface_ip = s.getsockname()[0]
s.close()
return iface_ip
def test_conn(Socket=socket.socket()):
''' not useful as a function apart but i must remember this one '''
if Socket._closed == False:
print('connection remains')
return True
else:
print('connection lost')
return False
toHex = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])
'''###################################################################'''
'''################# P2P CLIENT CLASS OBJECT DEFINED #################'''
class P2PSocketClient(socket.socket):
def __init__(self, \
IPClient=str('127.0.0.1'), \
PortClient=int(0), \
MaxBuffer=int(0)):
'''#########################################'''
'''UDP Sockets' prototypes are created here:'''
socket.socket.__init__(self, \
family=socket.AF_INET, \
proto=socket.IPPROTO_UDP, \
type=socket.SOCK_DGRAM)
'''#############################################'''
''' Initial Arguments' Values are checked here: '''
try:
isinstance(IPv4Address(IPClient), IPv4Address)
self.P2P_IPClient = IPClient
except AddressValueError as ErrorMessage:
print(ErrorMessage)
try:
isinstance(PortClient, int)
isinstance(MaxBuffer, int)
self.P2P_PortClient = PortClient
self.P2P_SocketClient = (IPv4Address(IPClient).compressed, \
self.P2P_PortClient)
self.P2P_SocketMaxBuffer = MaxBuffer
except TypeError as ErrorMessage:
print(ErrorMessage)
'''I will anticipate the code by asserting that
clients know already the server (but i wont change
functions' code for conformity):'''
self.P2P_IPServer = '192.168.56.101'
self.P2P_PortServer = 14
self.P2P_SocketServer = (self.P2P_IPServer, \
self.P2P_PortServer)
'''#################################################'''
'''A Centralized Buffer Stream within bytearray Memory
is initialized here:'''
self.P2P_SocketData = bytes()
self.P2P_SocketMemory = bytearray()
'''#######################'''
'''Setting Default Timeout'''
if socket.getdefaulttimeout() != None:
socket.setdefaulttimeout(None)
'''Binding created sockets:
By precaution, i manually reserved port 14 both tcp/udp
for my_py_server service in /etc/services file.'''
'''Creating bidirectional udp connectors for mutual communication'''
self.bind(self.P2P_SocketClient)
'''#######################################################'''
'''## Once Structure Generated, Connection starts with: ##'''
def startConnection(self):#,IPServer=str(),PortServer=int()):
#try:
# isinstance(IPv4Address(IPServer), IPv4Address)
# isinstance(PortServer, int)
# SocketServer = (IPv4Address(IPServer).compressed, PortServer)
# self.connect(SocketServer)
#except AddressValueError as ErrorMessage:
# print(ErrorMessage)
self.connect(self.P2P_SocketServer)
'''#################################################'''
'''To Safely Control Interruption of the Connection:'''
def stopConnection(self):
pass
'''#####################################################################'''
'''############### THREADING CLASSES FOR THE CLIENT-SIDE ###############'''
class ListenToLocalClient(threading.Thread):
def __init__(self, \
Socket=P2PSocketClient(), \
Data=bytes(), \
Memory=bytearray(), \
MaxBuffer=int()):
threading.Thread.__init__(self)
self.ConnectionSocket = Socket
self.ConnectionData = Data
self.ConnectionMemory = Memory
self.ConnectionMaxBuffer = self.ConnectionSocket.P2P_SocketMaxBuffer
self.HeaderServer = str('SERVER : ')
#if needed: for now, client dont.
#self.ConnectionLogFile = str(getcwd()+'/LogServer.log')
'''###########################################'''
'''## Automated Threading-Recv Function is: ##'''
def recv(self, \
recvSocket=P2PSocketClient(), \
recvData=bytes(), \
recvMemory=bytearray(), \
recvMaxBuffer=int()):
recvData = bytes(recvSocket.recv(recvMaxBuffer))
recvMemory.extend(recvData+b'\n')
print(self.HeaderServer+recvData.decode('utf-8'))
'''#############################################'''
'''##### Main Function to begin Threading: #####'''
def run(self):
try:
while self.ConnectionSocket._closed == False:
self.recv(recvSocket=self.ConnectionSocket, \
recvData=self.ConnectionData, \
recvMemory=self.ConnectionMemory, \
recvMaxBuffer=self.ConnectionMaxBuffer)
except KeyboardInterrupt:
self.ConnectionSocket.close()
'''###################################################################'''
'''###################################################################'''
class AnswerToServer(threading.Thread):
def __init__(self, \
Socket=P2PSocketClient()):
threading.Thread.__init__(self)
self.ConnectionSocket = Socket
'''###########################################'''
'''## Automated Threading-Send Function is: ##'''
def send(self, \
sendSocket=P2PSocketClient(), \
sendData=str()):
sendSocket.send(sendData.encode('utf-8'))
'''#############################################'''
'''##### Main Function to begin Threading: #####'''
def run(self):
try:
while self.ConnectionSocket._closed == False:
self.send(sendSocket=self.ConnectionSocket,sendData=input())
except KeyboardInterrupt:
self.ConnectionSocket.close()
'''###################################################################'''
'''###################################################################'''