0

I am working a new command line tool https://github.com/a7i7/HoldMyFile . But I am facing some problem with the following function:

def netcat(hostname, port, content):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((hostname, port))
    s.sendall(content)
    s.shutdown(socket.SHUT_WR)
    response = ''
    while 1:
        data = s.recv(1024)
        if data == "":
            break
        data = repr(data)
        response = response + data
    s.close()
    response = response[:-3][1:]
    return response

Whenever I call netcat('termbin.com',9999,content) and content has around 1 MB of data in it, the code exits with the following error message "socket.error: [Errno 32] Broken pipe" . Now if I send 1 MB of data from the linux shell using cat data.txt | nc termbin.com 9999 all the data is successfully sent. I even tried running the same command from the linux shell using os.system from python. still the broken pipe error is encountered. Kindly help me fix this

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
Afif
  • 66
  • 1
  • 4
  • In which line of code the exception raised? – Samuel Jul 28 '16 at 06:50
  • @Samuel s.sendall(content) but if I remove s.shutdown(socket.WR) then there is no exception, but all the data is not transferred either. – Afif Jul 28 '16 at 09:04
  • Did you checked this one? http://stackoverflow.com/questions/11866792/how-to-prevent-errno-32-broken-pipe – Samuel Jul 28 '16 at 11:46
  • 671872 bytes seem to be the limit. I read the link but still am not quite sure how to get this fixed. Is it likely that since python is slow termbin.com server might have closed the connection before all data has been transferred? – Afif Jul 28 '16 at 12:08
  • Yup. You can check this python netcat example though https://gist.github.com/f35a7252babdf77c8421 – Samuel Jul 28 '16 at 12:23
  • Tried it. Doesnt work. Same problem. Incomplete data transferred – Afif Jul 28 '16 at 12:44

0 Answers0