I wrote the following socket code for the (capture the flag) exercise, need help concatenating data received from the server, as seen in the screenshot.
I wrote the following socket code for the (capture the flag) exercise, need help concatenating data received from the server, as seen in the screenshot.
#####################################################################
import socket
import sys
import threading
HOST = 'cyber210.network'
PORT = 8002
BUFFER_SIZE = 4096
# Create a socket object and connect to the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with socket.socket() as s:
s.connect((HOST, PORT))
# Define a function to be called for each line received from the server
def handle_line(line):
if line.startswith('>'):
clean_line = line[1:]
try:
result = str(eval(clean_line)) + '\n'
print(clean_line, result)
s.sendall(result.encode())
except Exception as e:
print(f'Error evaluating line {clean_line}: {e}')
# Receive data from the server and process each line
while True:
data = s.recv(BUFFER_SIZE).decode()
if not data:
print('End of program')
sys.exit(1)
else:
#received_data += data
print(f'data recv: {data}')
for line in data.splitlines():
handle_line(line)
s.close()
##################################################################
Step by step
Solved in 2 steps