Tutorial: TFTP using Python

March 6, 2017 | Views: 10525

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

In this post, I’m going to share a short script which can be used to create you own TFTP server. You need to run the server.py on your server (it can be your local machine) and client.py on your client machine (i.e. your local machine). While both scripts are running, you can look at your server’s files and download them.

CODE: CLIENT.PY

import socket

def main():

sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

print 'Enter the client IP address i.e. 192.168.1.102',

print 'If server is your Local computer then you can enter 127.0.0.1'

while True:

ip=raw_input("Server's IP address: ")

try:

sock.connect((ip,5555))

break

except:

print '...Enter a valid IP address...'

help="Enter:nt'ls' for listnt'cwd' for current working directorynt'cd <directory name>' to change directorynt'download <file name>' to download a filent'quit' to Exit"

print help




while True:

cmd=raw_input('Enter a command: ')

if cmd=='quit':

sock.send('quit')

break

elif cmd=='ls' or cmd=='cwd' or (cmd[:2]=='cd' and cmd[2:3]==' ' and cmd[3:]!=''):

sock.send(cmd)

data=sock.recv(8096)

print data

elif (cmd[:8]=='download' and cmd[8:9]==' ' and cmd[9:]!=''):

sock.send(cmd)

data=sock.recv(8096)

if data=='__fail__':

print '...file not found...'

else:

file_name='downloaded_'+cmd[9:]

fo=open(file_name,'ab')

print '...downloading file Please wait...'

while data!='__done__':

fo.write(data)

data=sock.recv(8096)

fo.close()

print '...file downloaded...'

else:

print help

main()

 

SERVER.PY

 

import socket

import subprocess

import os

def main():

sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

sock.bind(('',5555))




while True:

cmd,addr=sock.recvfrom(2048)

if cmd=='quit':

break

elif cmd=='cwd':

sock.sendto(os.getcwd(),addr)

elif cmd[:2]=='cd' and cmd[2:3]==' ' and cmd[3:]!='':

try:

dir=cmd[3:]

os.chdir(os.path.join(os.getcwd(),dir))

sock.sendto('directory changed',addr)

except:

sock.sendto('fail to change directory',addr)

elif cmd=='ls':

process=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)

result=process.stdout.read()+' '

sock.sendto(result,addr)

elif cmd[:8]=='download' and cmd[8:9]==' ' and cmd[9:]!='':

file_name=cmd[9:]

try:

size=8096

fo=open(file_name,'rb')

data=fo.read(size)

while data!='':

sock.sendto(data,addr)

data=fo.read(size)

fo.close()

sock.sendto('__done__',addr)

except:

sock.sendto('__fail__',addr)

else:

pass




main()
Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
6 Comments
Page 2 of 2«12
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge

 

Cybrary|0P3N

Is Linux Worth Learning in 2020?
Views: 294 / December 14, 2019
How do I Get MTA Certified?
Views: 892 / December 12, 2019
How much does your PAM software really cost?
Views: 1343 / December 10, 2019
How Do I Get into Android Development?
Views: 1722 / December 8, 2019

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel