I'm currently working on a CTF challenge which runs on server, here is the part I need help with:
....
while True:
menu() #display 4 options
cmd = int(input(">> "))
if cmd == 1: #do something
elif cmd == 2: #do something
elif cmd == 3: #do something
elif cmd == 4: #do something
....
I tried to use socket.send()
and socket.recv()
to send and receive data, but it seems like my code only allows me to type in 1 value and then it freezes.
Here is my solve.py
:
import socket
HOST = '127.0.0.1'
PORT = 9000
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
received_data = s.recv(1024).decode() #this will receive the menu() display
print(received_data) #print it out
data = input() #I typed "1"
s.send(data.encode()) #send "1"
received_data = s.recv(1024).decode() #It should receive the option 1 action but it didn't
print('Received: ',received_data) #Print out '' when I force stop the program (nothing)
s.close()
When I force stop the server, it returned EOFError: EOF when reading a line
. I have found out that the problem was the input()
is in while loop. The loop need multiple input while my solve.py
only send 1 input.
But now I wonder what is the best solution for this. Using socket
is my priority, but others should also be fine ( pwn
, requests
, ...)
P/s: The server is fixed. Any change on the server.py
is not allowed.