Giter Site home page Giter Site logo

py3tftp's Introduction

Py3tftp

Py3tftp is an asynchronous TFTP server written in Python 3. It was written for the pure joy of working with Python 3 and implements RFC 1350 (except mail mode), RFC 2347 (options), RFC 2348 (blksize option), RFC 2349 (timeout, tsize), and RFC 7440 (windowsize) for RRQ. Additionally, it supports block number roll over, so files of any size can be transferred over.

While a toy project, it does adhere to enough of the standards to be useful in real life.

Some Py3k stuff it uses:

This project is in maintenance mode!

It is not actively developed, but it provides a solid minimum set of functionality and works on Python versions 3.5 though 3.9.

If you would like more functionality or if you find a bug, please open a PR :).

Installation

pip install py3tftp

Usage

Invoking pyt3tftp will start a server that will interact with the current working directory - it will read and write files from it so don't run it in a place with sensitive files!

TFTP has no security features, except for its simplicity:

  • It won't overwrite files.
  • Won't create non-existant directories.
  • Cannot write outside of the directory it's running from.
usage: py3tftp [-h] [--host HOST] [-p PORT] [--ack-timeout ACK_TIMEOUT]
                   [--timeout TIMEOUT] [-l FILE_LOG] [-v] [--version]

optional arguments:
  -h, --help            show this help message and exit
  --host HOST           IP of the interface the server will listen on.
                        Default: 0.0.0.0
  -p PORT, --port PORT  Port the server will listen on. Default: 9069. TFTP
                        standard-compliant port: 69 - requires superuser
                        privileges.
  --ack-timeout ACK_TIMEOUT
                        Timeout for each ACK of the lock-step. Default: 0.5.
  --timeout TIMEOUT     Timeout before the server gives up on a transfer and
                        closes the connection. Default: 3.
  -l FILE_LOG, --file-log FILE_LOG
                        Append output to log file.
  -v, --verbose         Enable debug-level logging.
  --version

Testing

I wrote some simple acceptance tests in tests/acceptance/*_test.py. The code is messy as it's meant to be thrown away.

# runs the server with python -m py3tftp
# runs unittests under tests/
# kills the server
./.travis/run.sh

Extending py3tftp

The way that this module works is that there's a subclass of BaseTFTPServerProtocol scheduled on the IO loop that listens for incoming TFTP requests and decides what kind of BaseTFTPProtocol to schedule on the IO loop to handle the incoming request.

The way this works in the default scenario is that TFTPServerProtocol listens to incoming requests. When a request comes in, it selects either the WRQProtocol or the RRQProtocol to create a task in the IO loop and passes the request to the selected protocol upon instantiation. From then on, the instantiated protocol handles all of the communication with the client and the TFTPServerProtocol goes back to listening for requests.

This amazing diagram illustrates the above process in the case of a RRQ request:

py3tftp rrq process diagram

Extending py3tftp is as easy as:

  • Subclassing BaseTFTPServerProtocol, mainly to implement the select_protocol method to select your custom protocol.
  • Subclassing either RRQProtocol or WRQProtocol to implement your own logic (new options, file handling, etc.) for standard WRQ and RRQ requests, OR...
  • Subclassing BaseTFTPProtocol to implement your own logic for a custom type of request.

Roadplan

(Note, this hasn't been updated in a few years)

  • fix off-by-one blksize error ie. if you transfer a file 1000 bytes long and set blksize to 1000 bytes, the server won't ack it.
  • Pull out file reader/writer from protocol classes.
  • Add tsize from RFC 2349 (added by schrd).
  • Add blksize, timeout, and tsize tests.
  • Possibly implement RFCs 906 and 951 for fun!
  • Refactor the code, get rid of some duplication, optimize some low-hanging fruit.

LICENSE

The MIT License (MIT)

Copyright (c) 2016-2021 sirMackk

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

py3tftp's People

Contributors

keenser avatar pkorpine avatar sirmackk avatar zometa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

py3tftp's Issues

How to implement it with code

i am a python newbie and would like to implement this with a program i write.
Can you give a simple example how to implement it (i understand how to use it as a standalone script), but can't use it within a program i am writing (import the module to a script does not reveal the libraries and methods) what am i missing?

--ack-timeout and --conn-timeout do not seem to work

Starting the TFTP server as follows does not change the default timeouts:
py3tftp -p 9069 --ack-timeout 5 --conn-timeout 10 -v

using Wireshark, I can see the timeouts are still ack-timeout=0.5 and conn-timeout=5.0.

The debug output of py3tftp shows the following:
2018-01-12 14:49:40,987 [INFO] Starting TFTP server on 0.0.0.0:9069
2018-01-12 14:49:40,988 [DEBUG] Using selector: KqueueSelector
2018-01-12 14:49:40,988 [INFO] Listening...
2018-01-12 14:50:02,492 [DEBUG] received: test.txtoctettsize0blksize508
2018-01-12 14:50:02,492 [DEBUG] Request: b'test.txt\x00octet\x00tsize\x000\x00blksize\x00508\x00'
2018-01-12 14:50:02,492 [DEBUG] b'tsize'
2018-01-12 14:50:02,493 [DEBUG] b'blksize'
2018-01-12 14:50:02,493 [DEBUG] packet type: RRQ
2018-01-12 14:50:02,493 [DEBUG] Request: b'test.txt\x00octet\x00tsize\x000\x00blksize\x00508\x00'
2018-01-12 14:50:02,494 [DEBUG] b'tsize'
2018-01-12 14:50:02,494 [DEBUG] b'blksize'
2018-01-12 14:50:02,494 [DEBUG] b'blksize'
2018-01-12 14:50:02,494 [INFO] Initiating RRQProtocol with ('127.0.0.1', 9997)
2018-01-12 14:50:02,495 [DEBUG] {b'blksize': 508}
2018-01-12 14:50:02,495 [DEBUG] {b'ack_timeout': 0.5, b'timeout': 5.0, b'blksize': 508, 'ack_timeout': 5.0, 'timeout': 10.0}
[DEBUG] {b'ack_timeout': 0.5, b'timeout': 5.0, b'blksize': 508, 'ack_timeout': 5.0, 'timeout': 10.0}
...

Any idea where the problem might be?
Regards,

sanitize_fname() behaves incorrectly on Windows

On windows, due to the explicit use of the forward slash in file_io.py line 13, the path for a given requested file is resolved incorrectly. For example, supplying an unqualified filename results in b'C:\\filename' regardless of the current working directory. A possible cross-platform replacement would be

import os
import os.path as opath


def sanitize_fname(fname):
    """
    Ensures that fname is a path under the current working directory.
    """
    root_dir = os.getcwd().encode('ascii')
    return opath.join(root_dir, opath.normpath(fname))

TODO: spring cleaning

It seems that people are still using this package. This means it needs little bit of spring cleaning:

  • update the setup.py to render the readme well on pypi.
  • update the readme if needed.
  • update supported python version to the newest one possible.

tftp: timeout

In [6]: !py3tftp -p 69
2023-03-13 12:38:47,982 [INFO] Starting TFTP server on 0.0.0.0:69
2023-03-13 12:38:47,983 [INFO] Listening...
2023-03-13 12:38:52,145 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:52,146 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:52,210 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:52,335 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:52,516 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:52,786 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:53,170 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:53,744 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:54,603 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:55,166 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:38:55,166 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:38:55,167 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:38:55,167 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:38:55,213 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:38:55,214 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:38:55,338 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:38:55,338 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:38:55,528 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:38:55,529 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:38:55,788 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:38:55,788 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:38:55,882 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:56,178 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:38:56,178 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:38:56,758 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:38:56,759 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:38:57,610 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:38:57,610 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:38:57,805 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:38:58,942 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:38:58,942 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:38:59,816 [INFO] Initiating RRQProtocol with ('192.168.1.7', 45690)
2023-03-13 12:39:00,808 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:39:00,808 [INFO] Connection to 192.168.1.7:45690 terminated
2023-03-13 12:39:02,818 [ERROR] Connection to ('192.168.1.7', 45690) timed out, "b'y.pub'" not transfered
2023-03-13 12:39:02,819 [INFO] Connection to 192.168.1.7:45690 terminated

client :

# tftp -g -r y.pub -l y.pub 192.168.1.10
tftp: timeout

Implement windowsize option

Hey sirMackk

I plan to implement TFTP windowsize option for the RRQProtocol:
https://tools.ietf.org/html/rfc7440

I already have implemented a working proof of concept. However, it requires several changes in the code. For the final implementation, I believe the best approach would be to directly modify the RRQProtocol class. The default windowsize=1 would make the code behave identically as it does now (except for a minor performance penalty, perhaps).

My hope is that you merge this changes in the master branch. That means that if you think that the proposed approach has no chances of being merged, I would like to get your feedback. :)

local file not found error

I'm running py3tftp on Windows 7 64bit and Python 3.7.0

When I run pytftp in a folder containing the file myip.py, I expect it to be able to serve that file.
However, when I ran a tftp client (on the same machine) and requested this file, I got a File Not Found error. Am I doing something wrong? Also, how do I quit without having to kill the process in Task Manager? Ctrl+C etc doesn't work.

Server invocation:

 Directory of c:\dev

12/16/2018  10:59 AM    <DIR>          .
12/16/2018  10:59 AM    <DIR>          ..
12/16/2018  10:43 AM               246 myip.py
12/16/2018  11:01 AM               334 myports.py
12/16/2018  11:01 AM    <DIR>          nppBackup
07/13/2009  10:20 PM    <DIR>          PerfLogs
               2 File(s)            580 bytes
               4 Dir(s)  60,627,619,840 bytes free

c:\dev>py3tftp -p 69 -v
2018-12-16 11:11:21,443 [INFO] Starting TFTP server on 0.0.0.0:69
2018-12-16 11:11:21,443 [DEBUG] Using selector: SelectSelector
2018-12-16 11:11:21,443 [INFO] Listening...
2018-12-16 11:11:25,819 [DEBUG] received:  ☺myip.py octet tsize 0
2018-12-16 11:11:25,819 [DEBUG] Request: b'myip.py\x00octet\x00tsize\x000\x00'
2018-12-16 11:11:25,819 [DEBUG] b'tsize'
2018-12-16 11:11:25,819 [DEBUG] packet type: RRQ
2018-12-16 11:11:25,819 [DEBUG] Request: b'myip.py\x00octet\x00tsize\x000\x00'
2018-12-16 11:11:25,819 [DEBUG] b'tsize'
2018-12-16 11:11:25,819 [DEBUG] b'tsize'
2018-12-16 11:11:25,819 [INFO] Initiating RRQProtocol with ('127.0.0.1', 54144)
2018-12-16 11:11:25,819 [DEBUG] Initializing file transfer to ('127.0.0.1', 54144)
2018-12-16 11:11:25,819 [DEBUG] Set protocol attributes as {b'ack_timeout': 0.5, b'timeout': 3.0, b'blksize': 512, b'windowsize': 1, b'tsize': 0}
2018-12-16 11:11:25,819 [ERROR] File "b'myip.py'" does not exist!
2018-12-16 11:11:25,819 [DEBUG] opening pkt: <py3tftp.tftp_packet.TFTPErrPacket object at 0x00000000032E19B0>
2018-12-16 11:11:25,819 [INFO] Closing connection to ('127.0.0.1', 54144) due to error. "b'myip.py'" Not transmitted.
2018-12-16 11:11:25,819 [INFO] Connection to 127.0.0.1:54144 terminated

client invocation (using mobaxterm cygwin-like shell):

➤ tftp -g -r myip.py 0.0.0.0
tftp: using blksize 512
using server '0.0.0.0:69', remote_file 'myip.py', local_file 'myip.py'
sending 24 bytes
00 01 6d 79 69 70 2e 70 79 00 6f 63 74 65 74 00 74 73 69 7a 65 00 30 00
received 19 bytes: 0005 0001
tftp: server error: (1) File not found
                                         

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.