I am working on a programming challenge and what I need to do is have python launch the program then attempt to connect to an open sock port to send data to the program I just launched.
The program in question when launched opens a listener on port 2222, when you send a string it will process it and give a response and exit. My intention is to automate sending data to this until I find the correct type of data to send to get the result I am expecting.
import socket
import os
msg = 0x09
os.system(r"C:/Users/user/Desktop/myfile.exe")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
TCP_IP = '127.0.0.1'
TCP_PORT = 2222
sock.connect((TCP_IP, TCP_PORT))
sock.send(str(msg))
result = sock.recv(1024)
sock.close
print result
The script will launch the exe but will not connect. If I manually open the file and then run the script it successfully communicates so I assumed it was a timing issue. I've used sleep delays of up to 20s and the script still hangs. I am unsure how I need to proceed to be able to make this work.
This is because
os.system
waits the program to finish so that it returns an exit value, see hereFor executing independent thread use subprocess