0

We have two servers (client-facing, and back-end database) between which we would like to transfer PDFs. Here's the data flow:

  1. User requests PDF from website.
  2. Site sends request to client-server.
  3. Client server requests PDF from back-end server (different IP).
  4. Back-end server sends PDF to client server.
  5. Client server sends PDF to website.

1-3 and 5 are all good, but #4 is the issue.

We're currently using Flask requests for our API calls and can transfer text and .csv easily, but binary files such as PDF are not working.

And no, I don't have any code, so take it easy on me. Just looking for a suggestion from someone who may have come across this issue.

elPastor
  • 8,435
  • 11
  • 53
  • 81
  • 1
    http://stackoverflow.com/questions/11017466/flask-return-image-created-from-database – maxymoo Dec 15 '16 at 00:13
  • 1
    Take a look at [SCP](https://pypi.python.org/pypi/scp) and [scpclient](https://pypi.python.org/pypi/scpclient) libraries – Moinuddin Quadri Dec 15 '16 at 00:14
  • @maxymoo et al. Thanks guys. Good info. Not sure I can mark the answer as *the* answer just yet, but will give it all a shot. – elPastor Dec 15 '16 at 00:25

2 Answers2

1

As you said you have no code, that's fine, but I can only give a few suggestions.

I'm not sure how you're sending your files, but I'm assuming that you're using pythons open function.

  1. Make sure you are reading the file as bytes (e.g. open('<pdf-file>','rb'))

  2. Cut the file up into chunks and send it as one file, this way it doesn't freeze or get stuck.

  3. Try smaller PDF files, if this works definitely try suggestion #2.

  4. Use threads, you can multitask with them.

  5. Have a download server, this can save memory and potentially save bandwidth. Also it also lets you skip the PDF send back, from flask.

  6. Don't use PDF files if you don't have to.

  7. Use a library to do it for you.

Hope this helps!

Rabin Lama Dong
  • 2,422
  • 1
  • 27
  • 33
Coolq B
  • 344
  • 5
  • 10
1

I wanted to share my solution to this, but give credit to @CoolqB for the answer. The key was including 'rb' to properly read the binary file and including the codecs library. Here are the final code snippets:

  1. Client request:

    response = requests.get('https://www.mywebsite.com/_api_call')

  2. Server response:

    f = codecs.open(file_name, 'rb').read() return f

  3. Client handle:

    with codecs.open(file_to_write, 'w') as f: f.write(response.content) f.close()

And all is right with the world.

elPastor
  • 8,435
  • 11
  • 53
  • 81