FreeDATA/socketclient.py

66 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 11 21:53:35 2020
@author: parallels
"""
import socket
import sys
2020-12-23 17:31:09 +00:00
import argparse
2021-01-05 14:03:41 +00:00
import random
2021-01-05 14:03:41 +00:00
#https://www.askpython.com/python/examples/generate-random-strings-in-python
def create_string(length):
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))
print("STR:" + str(random_string))
return random_string
2020-12-23 17:31:09 +00:00
#--------------------------------------------GET PARAMETER INPUTS
parser = argparse.ArgumentParser(description='Simons TEST TNC')
parser.add_argument('--port', dest="socket_port", default=9000, help="Set the port, the socket is listening on.", type=int)
2021-01-05 14:03:41 +00:00
#parser.add_argument('--data', dest="data", default=False, help="data", type=str)
parser.add_argument('--random', dest="datalength", default=False, help="data", type=int)
2020-12-23 17:31:09 +00:00
args = parser.parse_args()
2021-01-05 14:03:41 +00:00
data = create_string(args.datalength)
data = bytes("ACK:" + "!!!--" + data + "--!!!" + "\n", "utf-8")
2020-12-23 17:31:09 +00:00
2021-01-05 14:03:41 +00:00
print(data)
2020-12-23 17:31:09 +00:00
HOST, PORT = "localhost", args.socket_port
2021-01-05 14:03:41 +00:00
#data = args.data
# 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))
2021-01-05 14:03:41 +00:00
#sock.sendall(bytes(data + "\n", "utf-8"))
sock.sendall(data)
# Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
print("Sent: {}".format(data))
print("Received: {}".format(received))
2021-01-05 14:03:41 +00:00