<commit_before>#!/usr/bin/env python2
import socket,binascii,os
from math import ceil
from sys import exit

host = ''
port = 69

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))

def group(s, n): return [s[i:i+n] for i in xrange(0, len(s), n)]

while 1:
    try:
        message, address = s.recvfrom(8192)
        if message.startswith('\x00\x01'): #WRQ
           message=message.split('\x00')
           filename=message[1][1:]
           print address,"wants",repr(filename)
           if not os.path.exists('.'+filename):
              s.sendto('\x00\x05\x00\x01no such file exists',address)
              continue
           fsize=os.path.getsize('.'+filename)
           s.sendto('\x00\x06blksize\x001024\x00tsize\x00%s\x00' % fsize,address) ##512
        elif message.startswith('\x00\x04'): #OptACK
           f=open('.'+filename,'r')
           data=f.read()
           dataset=group(data,1024) ##512
           if len(dataset) > 65534: print "Won't work, too large... >64MB"
           for index,chunk in enumerate(dataset):
              index+=1
              try:
                 s.sendto('\x00\x03'+binascii.unhexlify(hex(index)[2:].rjust(4,'0'))+chunk,address)
              except TypeError:
                 break
              s.recvfrom(128)
    except KeyboardInterrupt:
        exit()<commit_msg>Made tftpd.py actually work for larger files, wasnt sending eof properly.<commit_after>#!/usr/bin/env python2
import socket,binascii,os
from math import ceil
from sys import exit

host = ''
port = 69

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))

def group(s, n): return [s[i:i+n] for i in xrange(0, len(s), n)]
last=()
import time
while 1:
    try:
        message, address = s.recvfrom(8192)
        print repr(message)
        if last == address: continue
        if message.startswith('\x00\x01'): #WRQ
           message=message.split('\x00')
           filename=message[1][1:]
           print address,"wants",repr(filename)
           if not os.path.exists('.'+filename):
              s.sendto('\x00\x05\x00\x01no such file exists',address)
              continue
           fsize=os.path.getsize('.'+filename)
           s.sendto('\x00\x06blksize\x00512\x00tsize\x00%s\x00' % fsize,address)
        elif message.startswith('\x00\x04'): #OptACK
           last=address
           f=open('.'+filename,'r')
           data=f.read()
           dataset=group(data,512)
           if len(dataset) > 65534: print "Won't work, too large... >64MB"
           for index,chunk in enumerate(dataset):
              try:
                 s.sendto('\x00\x03'+binascii.unhexlify(hex(index+1)[2:].rjust(4,'0'))+chunk,address)
              except TypeError:
                 break
              s.recvfrom(128)
           s.sendto('\x00\x03'+binascii.unhexlify(hex(index+2)[2:].rjust(4,'0')),address)
           f.close()
    except KeyboardInterrupt:
        exit()
{'fraction_non_alphanumeric': 0.1478318002628121, 'fraction_numerical': 0.04862023653088042, 'mean_word_length': 2.3278688524590163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6128329', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1037, 'n_words': 196}