Skip to content

Commit

Permalink
error syntc fix
Browse files Browse the repository at this point in the history
  • Loading branch information
uri-tech committed Jun 22, 2022
1 parent 2367b64 commit 5f79ce6
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 191 deletions.
2 changes: 1 addition & 1 deletion Analysis_Server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

if __name__ == '__main__':
try:
# Create the server, binding to ip HOST on PORT
# Create the server, binding to ip HOST on PORT
with socketserver.ThreadingTCPServer(("0.0.0.0", int(PORT_SERVER)), CustomTCPHandler) as server:
logger.warning(f"Listening on 0.0.0.0 in port {PORT_SERVER}", extra=d)
# Activate the server; this will keep running until interrupt the program with Ctrl-C
Expand Down
9 changes: 5 additions & 4 deletions Analysis_Server/modulos/network.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

try:
# import logging
import socket
import threading
# import socket
# import threading
import socketserver
# import sys
except Exception as ex:
Expand Down Expand Up @@ -81,7 +81,8 @@ def handle(self):
# # header_json = json.dumps(header_dic)
# print(stderr)
# # header_bytes = header_json.encode('utf-8')
# # self.request.send(struct.pack('i', len(header_bytes))) # This sent a fixed number of bytes in the past 4, so the client can receive four bytes for the first time
# # self.request.send(struct.pack('i', len(header_bytes))) # This sent a fixed number of bytes in the past 4,
# so the client can receive four bytes for the first time
# # self.request.send(header_bytes)
# # self.request.send(stdout)
# # self.request.send(stderr)
Expand All @@ -107,4 +108,4 @@ def handle(self):
# # to the client
# self.wfile.write(self.data.upper())
# except Exception as ex:
# print(f"ERROR:\n{ex}")
# print(f"ERROR:\n{ex}")
5 changes: 2 additions & 3 deletions Area_High/modulos/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ class CustomTCPHandler(socketserver.BaseRequestHandler):
def handle(self):
try:
# self.request is the TCP socket connected to the client

self.data = self.request.recv(1024).strip()

print("{} wrote:".format(self.client_address[1]))
print(self.data)
low_db = 2
Expand Down Expand Up @@ -70,4 +69,4 @@ def forward_socket(src_port, dst_port, data):
sock.sendall(data)
print(f"Sent FROM: {src_port} ==> TO: {dst_port}")
except Exception as ex:
print(f"ERROR:\n{ex}")
print(f"ERROR:\n{ex}")
2 changes: 1 addition & 1 deletion Area_Low/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

if __name__ == '__main__':
try:
# Create the server, binding to ip HOST on PORT
# Create the server, binding to ip HOST on PORT
with socketserver.ThreadingTCPServer(("0.0.0.0", int(PORT_LOW_AREA)), CustomTCPHandler) as server:
logger.warning(f"Listening on '0.0.0.0' in port {int(PORT_LOW_AREA)}", extra=d)
# Activate the server; this will keep running until interrupt the program with Ctrl-C
Expand Down
48 changes: 3 additions & 45 deletions Area_Low/modulos/network.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

from HOST_PORT import HOSTPORT

try:
# import logging
import socket
import threading
# import threading
import socketserver
# import sys
except Exception as ex:
Expand Down Expand Up @@ -32,18 +29,16 @@ def handle(self):

low_db = 2
data_byte = lower_snr(self.data, low_db)
index_start = str(data_byte).find('dBm',0,10)+3
index_start = str(data_byte).find('dBm', 0, 10)+3
client_id = int(str(data_byte)[index_start:index_start+10])

print(data_byte)
# Send forward the same data, with lower snr
if client_sid < client_id < client_sid + client_max_device:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(("localhost", 8014))
sock.sendall(data_byte)



# if(self.client_address[1] == HOSTPORT['Client']):
# #"Forward Higher"
# #forward_socket(HOSTPORT['Area_Low_OUT'], HOSTPORT['Area_High'], data_byte)
Expand Down Expand Up @@ -82,43 +77,6 @@ def handle(self):
# print(f"ERROR:\n{ex}")


# class MyTcpHandler(socketserver.BaseRequestHandler):
# # BaseRequestHandler is specifically used to process communication-related information
# def handle(self):
# try:
# # Here must define a handle method, and the method name must be handle, sockerserver will automatically call the handle method
# print(self.request) # The self.request here is equivalent to the conn object we saw before (conn,client_addr=server.accept())
# while True:
# try:
# recv_cliend_cmd = self.request.recv(1024) # Receive instructions from the client
# if not recv_cliend_cmd:
# break
# # Next, we will process the instructions sent by the client
# obj = subprocess.Popen(recv_cliend_cmd.decode('utf-8'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# stdout = obj.stdout.read()
# stderr = obj.stderr.read()
# # Let's create the header (write it at will), here we really use the total_size in the dictionary
# # header_dic={
# # 'total_size':len(stdout)+len(stderr),
# # 'filename':'xxx.mp4',
# # 'md5sum':'8f6fbf8347faa4924a76856701edb0f3'
# # }
# # header_json = json.dumps(header_dic)
# print(stderr)
# # header_bytes = header_json.encode('utf-8')
# # self.request.send(struct.pack('i', len(header_bytes))) # This sent a fixed number of bytes in the past 4, so the client can receive four bytes for the first time
# # self.request.send(header_bytes)
# # self.request.send(stdout)
# # self.request.send(stderr)
# self.request.sendall(self.data.upper())

# except ConnectionResetError:
# break
# self.request.close()
# except Exception as ex:
# print(f"ERROR:\n{ex}")


# class MyTCPHandler(socketserver.StreamRequestHandler):
# def handle(self):
# try:
Expand Down
9 changes: 5 additions & 4 deletions Attacker/modulos/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
try:
# import logging
import socket
import threading
# import threading
import socketserver
# import sys
except Exception as ex:
Expand Down Expand Up @@ -36,9 +36,9 @@ def handle(self):
# else:
# # Forword Lower
# forward_socket(HOSTPORT['Attacker_OUT'],HOSTPORT['Area_Low'],data_byte)
index_start = str(data_byte).find('dBm',0,10)+3
index_start = str(data_byte).find('dBm', 0, 10)+3
client_id = int(str(data_byte)[index_start:index_start+10])

print(data_byte)
# Send forward the same data, with lower snr
if client_sid < client_id < client_sid + client_max_device:
Expand Down Expand Up @@ -101,7 +101,8 @@ def handle(self):
# # header_json = json.dumps(header_dic)
# print(stderr)
# # header_bytes = header_json.encode('utf-8')
# # self.request.send(struct.pack('i', len(header_bytes))) # This sent a fixed number of bytes in the past 4, so the client can receive four bytes for the first time
# # self.request.send(struct.pack('i', len(header_bytes))) # This sent a fixed number of bytes in the past 4,
# so the client can receive four bytes for the first time
# # self.request.send(header_bytes)
# # self.request.send(stdout)
# # self.request.send(stderr)
Expand Down
5 changes: 3 additions & 2 deletions Client/modulos/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def client(ip, port, message):
# # header_json = json.dumps(header_dic)
# print(stderr)
# # header_bytes = header_json.encode('utf-8')
# # self.request.send(struct.pack('i', len(header_bytes))) # This sent a fixed number of bytes in the past 4, so the client can receive four bytes for the first time
# # self.request.send(struct.pack('i', len(header_bytes))) # This sent a fixed number of bytes in the past 4,
# so the client can receive four bytes for the first time
# # self.request.send(header_bytes)
# # self.request.send(stdout)
# # self.request.send(stderr)
Expand All @@ -107,4 +108,4 @@ def client(ip, port, message):
# # to the client
# self.wfile.write(self.data.upper())
# except Exception as ex:
# print(f"ERROR:\n{ex}")
# print(f"ERROR:\n{ex}")
1 change: 0 additions & 1 deletion Satellite/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# assert requests.get('https://github.com/p-2022-091/modulos/network.py').status_code == 200
import signal


# # formating the log
# import logging
# FORMAT = '%(asctime)-15s %(nodeip)s %(type)-8s, message: %(message)s'
Expand Down
132 changes: 2 additions & 130 deletions Satellite/modulos/network.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<<<<<<< HEAD
try:
# import logging
import socket
Expand Down Expand Up @@ -96,7 +95,8 @@ def client(ip, port, message):
# # header_json = json.dumps(header_dic)
# print(stderr)
# # header_bytes = header_json.encode('utf-8')
# # self.request.send(struct.pack('i', len(header_bytes))) # This sent a fixed number of bytes in the past 4, so the client can receive four bytes for the first time
# # self.request.send(struct.pack('i', len(header_bytes))) # This sent a fixed number of bytes in the past 4,
# so the client can receive four bytes for the first time
# # self.request.send(header_bytes)
# # self.request.send(stdout)
# # self.request.send(stderr)
Expand All @@ -123,131 +123,3 @@ def handle(self):
self.wfile.write(self.data.upper())
except Exception as ex:
print(f"ERROR:\n{ex}")
=======
try:
# import logging
import socket
import threading
import socketserver
from time import time
import random
RANGE_CONNECTION_TIME: tuple = tuple((114, 124))
# import sys
except Exception as ex:
raise Exception(f"ERROR:\n{ex}")


class CustomTCPHandler(socketserver.BaseRequestHandler):
"""
The request handler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
try:
if hasattr(self, 'startTime') is False:
self.startTime = time()
print(self.startTime)
daltaRange = RANGE_CONNECTION_TIME[1] - RANGE_CONNECTION_TIME[0]
RANGE_CONNECTION_TIME[0]-RANGE_CONNECTION_TIME[1]
maxConnectionTime: float = random.gauss(RANGE_CONNECTION_TIME[0]+daltaRange, daltaRange)
while(maxConnectionTime < RANGE_CONNECTION_TIME[0] or maxConnectionTime > RANGE_CONNECTION_TIME[1]):
maxConnectionTime: float = random.gauss(RANGE_CONNECTION_TIME[0]+daltaRange, daltaRange)
# print(f"maxConnectionTime: {maxConnectionTime}")
if(time()-self.startTime < maxConnectionTime):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print("{} wrote:".format(self.client_address[0]))
print(self.data)
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
# self.request is the TCP socket connected to the client
else:
print(f"finish transmition with client")
self.finish()
except Exception as ex:
print(f"ERROR:\n{ex}")


class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):

def handle(self):
try:
data = str(self.request.recv(1024), 'ascii')
cur_thread = threading.current_thread()
response = bytes("{}: {}".format(cur_thread.name, data), 'ascii')
self.request.sendall(response)
except Exception as ex:
print(f"ERROR:\n{ex}")


class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass


def client(ip, port, message):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((ip, port))
sock.sendall(bytes(message, 'ascii'))
response = str(sock.recv(1024), 'ascii')
print("Received: {}".format(response))
except Exception as ex:
print(f"ERROR:\n{ex}")



class MyTcpHandler(socketserver.BaseRequestHandler):
# BaseRequestHandler is specifically used to process communication-related information
def handle(self):
try:
# Here must define a handle method, and the method name must be handle, sockerserver will automatically call the handle method
print(self.request) # The self.request here is equivalent to the conn object we saw before (conn,client_addr=server.accept())
while True:
try:
recv_cliend_cmd = self.request.recv(1024) # Receive instructions from the client
if not recv_cliend_cmd:
break
# Next, we will process the instructions sent by the client
obj = subprocess.Popen(recv_cliend_cmd.decode('utf-8'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = obj.stdout.read()
stderr = obj.stderr.read()
# Let's create the header (write it at will), here we really use the total_size in the dictionary
# header_dic={
# 'total_size':len(stdout)+len(stderr),
# 'filename':'xxx.mp4',
# 'md5sum':'8f6fbf8347faa4924a76856701edb0f3'
# }
# header_json = json.dumps(header_dic)
print(stderr)
# header_bytes = header_json.encode('utf-8')
# self.request.send(struct.pack('i', len(header_bytes))) # This sent a fixed number of bytes in the past 4, so the client can receive four bytes for the first time
# self.request.send(header_bytes)
# self.request.send(stdout)
# self.request.send(stderr)
self.request.sendall(self.data.upper())

except ConnectionResetError:
break
self.request.close()
except Exception as ex:
print(f"ERROR:\n{ex}")


class MyTCPHandler2(socketserver.StreamRequestHandler):

def handle(self):
try:
# self.rfile is a file-like object created by the handler;
# we can now use e.g. readline() instead of raw recv() calls
self.data = self.rfile.readline().strip()
print("{} wrote:".format(self.client_address[0]))
print(self.data)
# Likewise, self.wfile is a file-like object used to write back
# to the client
self.wfile.write(self.data.upper())
except Exception as ex:
print(f"ERROR:\n{ex}")
>>>>>>> dc8212cf7e287790ab842329a7fd388dfa8a04bc

0 comments on commit 5f79ce6

Please sign in to comment.