-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Boring Network Tool #34
base: master
Are you sure you want to change the base?
Changes from all commits
fd0a888
f8101fd
ed45506
df99928
90850f3
65770ce
3d24e86
8f1b399
305966a
f5cb9c8
b0f9593
223a8fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
##Boring Network Tool | ||
|
||
The file main.py can be used to send either | ||
TCP or UDP messages to a given hostname and port, specified via the config json. | ||
|
||
This script does not require any external packages, aside from standard python 3.7 or higher. | ||
|
||
###Usage | ||
|
||
sending_config.json | ||
has a similar configuration to the json in Pidaq/Examples/CanSender, and can be used to specify the | ||
messages to send, and whether to send them via TCP or UDP. The frequency with which to send the | ||
messages can also be specified. Finally, the port and hostname are specified in the json as well. | ||
|
||
tcp_server.py implements a simple TCP server to verify the functionality of the script. | ||
|
||
udp_server.py implements a simple UDP server to verify the functionality of the script. | ||
|
||
For both server scripts, to receive any messages and print them out, simply use localhost | ||
and port 8888 to connect to the server after running it |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import socket | ||
import sys | ||
import time | ||
import json | ||
|
||
|
||
class Message: | ||
def __init__(self, data): | ||
self.data = data | ||
|
||
def to_string(self) -> str: | ||
return "%s \n" % str(self.data) | ||
Comment on lines
+7
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this a lot, very clean |
||
|
||
|
||
def create_messages(raw_messages): | ||
msgs = [] | ||
for message in raw_messages: | ||
msgs.append(Message(data=message["data"])) | ||
return msgs | ||
|
||
|
||
mjswen0923 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def send_with_udp(msgconfig, host, port): | ||
try: | ||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
except socket.error: | ||
print('Failed to create socket') | ||
sys.exit() | ||
msgs = create_messages(msgconfig["config"]["messages"]) | ||
msg_frequency = msgconfig["config"]["messageFrequency"] | ||
for message in msgs: | ||
time.sleep(1.0 / msg_frequency) | ||
try: | ||
# Set the whole string | ||
s.sendto(bytes(message.to_string(), 'utf-8'), (host, port)) | ||
print("message sent: %s" % message.to_string()) | ||
# receive data from client (data, addr) | ||
if msgconfig["config"]["sendContinuous"] is False: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are probably gonna want to properly send the messages repetitively indefinitely |
||
d = s.recvfrom(1024) | ||
reply = d[0] | ||
print('Server reply : %s' % reply.decode('utf-8')) | ||
except socket.error as msg: | ||
print('Error Code : %s Message : %s ' % (str(msg[0]), msg[1])) | ||
sys.exit() | ||
|
||
|
||
def send_with_tcp(msgconfig, host, port): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be nice for TCP to have the option for the network tool to act as either the server or the client There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you did this, you might be able to eliminate the tcp_server.py script altogether and use 2 instances of main.py, one configured as server and one configured as client, to test |
||
try: | ||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
s.connect((host, port)) | ||
except socket.error: | ||
print('Failed to create socket') | ||
sys.exit() | ||
msgs = create_messages(msgconfig["config"]["messages"]) | ||
msg_frequency = msgconfig["config"]["messageFrequency"] | ||
for message in msgs: | ||
time.sleep(1.0 / msg_frequency) | ||
try: | ||
# Set the whole string | ||
s.sendall(bytes(message.to_string(), 'utf-8')) | ||
print("message sent: %s" % message.to_string()) | ||
# receive data from client (data, addr) | ||
d = s.recv(1024) | ||
print('Server reply : %s' % d.decode('utf-8')) | ||
except socket.error as msg: | ||
print('Error Code : %s Message : %s ' % (str(msg[0]), msg[1])) | ||
s.close() | ||
sys.exit() | ||
s.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
with open('sending_config.json', mode='r') as json_config: | ||
msg_config = json.load(json_config) | ||
host = msg_config["config"]["ip"] | ||
port = msg_config["config"]["port"] | ||
useTCP = msg_config["config"]["useTCP"] | ||
sendContinuous = msg_config["config"]["sendContinuous"] | ||
print("Using TCP: %s" % str(useTCP)) | ||
print("Waiting for response: %s" % str(sendContinuous)) | ||
if useTCP: | ||
send_with_tcp(msg_config, host, port) | ||
else: | ||
send_with_udp(msg_config, host, port) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"config": { | ||
"ip": "localhost", | ||
"port": 8888, | ||
"useTCP": false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it might be better if this is commsType, or something similar, and then it parses for TCP or UDP as the value. If you make the option to act as the tcp server, you could then have a setting called servePort which would determine that. |
||
"sendContinuous": true, | ||
"messageFrequency": 5, | ||
"messages": [ | ||
{ | ||
"data": [ | ||
5, | ||
6, | ||
7, | ||
8, | ||
9, | ||
10 | ||
] | ||
}, | ||
{ | ||
"data": [ | ||
72, | ||
54, | ||
32, | ||
87 | ||
] | ||
}, | ||
{ | ||
"data": [ | ||
100, | ||
253, | ||
0, | ||
87 | ||
] | ||
}, | ||
{ | ||
"data": [ | ||
87 | ||
] | ||
}, | ||
{ | ||
"data": [ | ||
98, | ||
56, | ||
182, | ||
78, | ||
28, | ||
37, | ||
16, | ||
82 | ||
] | ||
} | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import socket | ||
import sys | ||
|
||
HOST = 'localhost' | ||
PORT = 8888 | ||
|
||
try: | ||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
print('Socket created') | ||
except socket.error as msg: | ||
print('Failed to create socket. Error Code : %s Message : %s' % (str(msg[0]), msg[1])) | ||
sys.exit() | ||
|
||
try: | ||
s.bind((HOST, PORT)) | ||
except socket.error as msg: | ||
print('Bind failed. Error Code : %s Message : %s' % (str(msg[0]), msg[1])) | ||
sys.exit() | ||
|
||
|
||
s.listen(1) | ||
conn, addr = s.accept() | ||
while 1: | ||
data = conn.recv(1024) | ||
print("Data received: %s" % data.decode('utf-8')) | ||
if not data: | ||
break | ||
conn.sendall(data) | ||
conn.close() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import socket | ||
import sys | ||
|
||
HOST = 'localhost' | ||
PORT = 8888 | ||
|
||
try: | ||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
print('Socket created') | ||
except socket.error as msg: | ||
print('Failed to create socket. Error Code : %s Message : %s' % (str(msg[0]), msg[1])) | ||
sys.exit() | ||
|
||
try: | ||
s.bind((HOST, PORT)) | ||
except socket.error as msg: | ||
print('Bind failed. Error Code : %s Message : %s' % (str(msg[0]), msg[1])) | ||
sys.exit() | ||
|
||
while 1: | ||
d = s.recvfrom(1024) | ||
data = d[0] | ||
addr = d[1] | ||
if not data: | ||
break | ||
|
||
print("Data received: %s" % data.decode('utf-8')) | ||
reply = 'OK... %s' % data.decode('utf-8') | ||
s.sendto(bytes(reply, 'utf-8'), addr) | ||
print('Message[%s:%s] - %s' % (addr[0], str(addr[1]), data.decode('utf-8').strip())) | ||
|
||
s.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably want to order these in alphabetical order