Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File Transfer
#1
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()
Reply


Messages In This Thread
File Transfer - by deezy - Nov-09-2017, 05:39 PM
RE: File Transfer - by heiner55 - Nov-10-2017, 06:15 AM
RE: File Transfer - by Jaylord - Feb-24-2019, 04:54 PM
RE: File Transfer - by Jaylord - Feb-25-2019, 09:44 AM
RE: File Transfer - by Jaylord - Feb-25-2019, 02:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  File transfer with xmodem CRC (1024) checksum Jhonbovi 3 8,298 Nov-08-2018, 09:01 AM
Last Post: Larz60+
  file transfer with sockets sinister88 1 6,452 Nov-11-2017, 03:29 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020