2021-03-01 07:38:28 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Created on Fri Dec 11 21:53:35 2020
|
|
|
|
|
|
|
|
@author: parallels
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import random
|
2022-05-28 02:17:15 +00:00
|
|
|
import socket
|
2021-03-01 07:38:28 +00:00
|
|
|
|
2022-05-23 12:26:14 +00:00
|
|
|
|
2021-03-01 07:38:28 +00:00
|
|
|
def create_string(length):
|
2022-05-23 12:26:14 +00:00
|
|
|
# https://www.askpython.com/python/examples/generate-random-strings-in-python
|
|
|
|
|
2021-03-01 07:38:28 +00:00
|
|
|
random_string = ''
|
|
|
|
for _ in range(length):
|
|
|
|
# Considering only upper and lowercase letters
|
|
|
|
random_integer = random.randint(97, 97 + 26 - 1)
|
|
|
|
flip_bit = random.randint(0, 1)
|
|
|
|
# Convert to lowercase if the flip bit is on
|
|
|
|
random_integer = random_integer - 32 if flip_bit == 1 else random_integer
|
|
|
|
# Keep appending random characters using chr(x)
|
|
|
|
random_string += (chr(random_integer))
|
2022-05-28 02:17:15 +00:00
|
|
|
print(f"STR: {random_string!s}")
|
|
|
|
|
2021-03-01 07:38:28 +00:00
|
|
|
return random_string
|
|
|
|
|
2022-05-23 12:26:14 +00:00
|
|
|
|
|
|
|
# --------------------------------------------GET PARAMETER INPUTS
|
2021-03-01 07:38:28 +00:00
|
|
|
parser = argparse.ArgumentParser(description='Simons TEST TNC')
|
2022-05-28 02:17:15 +00:00
|
|
|
parser.add_argument('--port', dest="socket_port", default=9000, help="Set the port, the socket is listening on.", type=int)
|
2022-05-23 12:26:14 +00:00
|
|
|
# parser.add_argument('--data', dest="data", default=False, help="data", type=str)
|
2021-03-01 07:38:28 +00:00
|
|
|
parser.add_argument('--random', dest="datalength", default=False, help="data", type=int)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
data = create_string(args.datalength)
|
|
|
|
data = bytes("ARQ:DATA:" + "" + data + "" + "\n", "utf-8")
|
|
|
|
|
|
|
|
HOST, PORT = "localhost", args.socket_port
|
2022-05-23 12:26:14 +00:00
|
|
|
# data = args.data
|
2021-03-01 07:38:28 +00:00
|
|
|
|
|
|
|
# Create a socket (SOCK_STREAM means a TCP socket)
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
|
|
# Connect to server and send data
|
|
|
|
sock.connect((HOST, PORT))
|
2022-05-23 12:26:14 +00:00
|
|
|
# sock.sendall(bytes(data + "\n", "utf-8"))
|
2021-03-01 07:38:28 +00:00
|
|
|
sock.sendall(data)
|
|
|
|
# Receive data from the server and shut down
|
|
|
|
received = str(sock.recv(1024), "utf-8")
|
|
|
|
|
2022-05-28 02:17:15 +00:00
|
|
|
print(f"Sent: {data}")
|