I'm very new to learning Python and I'm trying to have a client send a text file to a server and have the server write it to another text file named "text.txt". I've kinda got it to work but the issue is that the only way the server writes it to the text file is if it exits. Also in order for the "text.txt" file to show me it's contents I have to click a "Reload" button. I just wanted to try and make it be a little more automated and without having to kill the server every time. I know this probably is really awful looking but like I said I am very new.
SERVER:
#!/usr/bin/env python3
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
tst = open('text.txt','w')
while(1):
s.listen(1)
# print type(s.accept())
conn, addr = s.accept()
print ('Connected by', addr)
while 1:
data = conn.recv(1024)
tst.write(data.decode())
if not data: break
conn.sendall(data)
if data: exit()
conn.close()
tst.close()
CLIENT:
#!/usr/bin/env python3
import socket
import sys
import subprocess
HOST = '172.19.40.52' # The target IP address
PORT = 50007 # The target port as used by the server
DATA = open('goodpasswd.txt','r')
BDATA = DATA.read().encode()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send(BDATA) #Put the pattern you want to send here.
data = s.recv(1024)
s.close()
SERVER:
#!/usr/bin/env python3
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
tst = open('text.txt','w')
while(1):
s.listen(1)
# print type(s.accept())
conn, addr = s.accept()
print ('Connected by', addr)
while 1:
data = conn.recv(1024)
tst.write(data.decode())
if not data: break
conn.sendall(data)
if data: exit()
conn.close()
tst.close()
CLIENT:
#!/usr/bin/env python3
import socket
import sys
import subprocess
HOST = '172.19.40.52' # The target IP address
PORT = 50007 # The target port as used by the server
DATA = open('goodpasswd.txt','r')
BDATA = DATA.read().encode()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send(BDATA) #Put the pattern you want to send here.
data = s.recv(1024)
s.close()