Forums

Uploading, Processing & Downloading Files

I'm trying to Upload a PDF, Convert the PDF into a Word Document, & Download the Word Document in PythonAnywhere. The processes work in my local machine, but the conversion and downloading process is not working when deployed in PythonAnywhere.

Here is the error I'm receiving:

"Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Please advise on how to fix this issue with downloading in PythonAnywhere.

What errors do you see in your app's error log on PythonAnywhere? (Logs are linked on the Web page.)

Here is the error I'm receiving:

doc = aw.Document(upload_folder + "/{}".format(f.filename))
RuntimeError: Proxy error(FileNotFoundException): Could not find file '.......pdf'.

[edit by admin: formatting]

Is that the exact error message that you're seeing? Or is the "......." a placeholder for a filename/path that you don't want to post publicly?

Hi,

This is the exact error message I'm getting with the "....." being a placeholder for a filepath.

Check whether the file is actually there and also see https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

So I have the path of my upload and download directory as follows. The file gets saved in the uploads folder as I could check it.

upload_folder = os.path.dirname(os.path.abspath(__file__)) + '/uploads'
download_folder = os.path.dirname(os.path.abspath(__file__)) + '/downloads'
if not os.path.exists(upload_folder):
    os.mkdir(upload_folder)
if not os.path.exists(download_folder):
    os.mkdir(download_folder)

However, I'm getting the error message below and also no file is going to the download folder. Please help.

**Something went wrong :-(
Something went wrong while trying to load this site; please try again later.
Debugging tips
If this is your site, and you just reloaded it, then the problem might simply be that it hasn't loaded up yet. Try refreshing this page and see if this message disappears.
If you keep getting this message, you should check your site's server and error logs for any messages.
Error code: 502-backend**

Try printing out the path of the upload and directories, also the file that you're using for the Document. The error suggests that the document filepath is just a load of dots

Thank you for your suggestion. I printed out the path of my upload_folder. As you can see in the code below, the path of the upload folder is hardcoded. However, the issue is that the pdf is uploaded after creating the upload_folder Right now the processed word document is supposed to go to the same upload_folder. But no word document is being generated. It still continues to give the same Error code: 502-backend as shown above.

upload_folder = "/home/softwaretoolbelt/Software-Tool-Belt/uploads" # Absolute Path of upload_folder.
...
...
@app.route('/pdftoword', methods = ['GET', 'POST'])
def uploadfile():
   if request.method == 'POST': # check if the method is post
      f = request.files['file'] # get the file from the files object
      # Saving the file in the required destination
      if allowed_file(f.filename):
         # Save pdf into upload_folder.
         f.save(os.path.join(app.config['UPLOAD_FOLDER'] ,secure_filename(f.filename))) # this will secure the file
         print("filename: ", f.filename)
         filename = f.filename.split(".")
         # Convert pdf to word document and save into upload_folder
         doc = aw.Document(upload_folder + "/{}".format(f.filename))
         doc.save(upload_folder + "/{}.docx".format(filename[0]))
         return send_file(upload_folder + "/{}.docx".format(filename[0]), 
                          as_attachment=True)
      else:
         return 'The file extension is not allowed'

[formatted by admin]

Could you check the error messages at the bottom of your web app's error log?

There is no error log created when I'm getting the following message in the browser. Probably there is no error log entry even after refreshing the browser. Please help.

Something went wrong :-( Something went wrong while trying to load this site; please try again later. Debugging tips If this is your site, and you just reloaded it, then the problem might simply be that it hasn't loaded up yet. Try refreshing this page and see if this message disappears. If you keep getting this message, you should check your site's server and error logs for any messages. Error code: 502-backend

It looks like your app is loading now, is the issue resolved?

No, PDF to Word conversion component of the app is not loading after the pdf file is submitted for processing, although the app works in local drive. The issue is specific to pythonanywhere server. The other parts of the app is working fine. Please check the code above associated with nonworking part of the app and let me know.

On the web app page there are a couple links (about half way down the page) to the logs generated by your web app. They are access.log, error.log and server. They contain the following: - access.log: contains logs of the which requests have reached the app, the response code and the time taken for the response to return - error.log: contains all the errors generated by the web app. If your app isn't working as expected then this is probably the place to check first - server.log: contains some production server logs and anything you've logged or printed in your app code

The following messages are generated in the server log that were causing the 502-backend error.

2024-11-09 00:50:50 DAMN ! worker 2 (pid: 17) died, killed by signal 11 :( trying respawn ... 2024-11-09 00:50:50 Respawned uWSGI worker 2 (new pid: 23) 2024-11-09 00:50:50 spawned 2 offload threads for uWSGI worker 2

To fix that issue, I looked at the following link

https://www.pythonanywhere.com/forums/topic/1100/.

From the discussion in the forum, I would need to comment out app.run() as suggested by another PythonAnywhere staff (https://www.pythonanywhere.com/forums/topic/1100/#id_post_7739). However, the same 502 backend error is still generated. Please help.

We have a help page that covers how to diagnose 502 errors.

After some debugging from the help page and printing some timestamps in my code, I find my app is not working from the following line.

doc = aw.Document(upload_folder + "/{}".format(f.filename)) where aw represents aspose.words

Probably aspose_words is not working on PythonAnywhere. The version of aspose_words I have in my requirements.txt is aspose_words==24.10.0. The app is running on Python 3.10.5.

Can anyone guide me as to whether I need to change or upgrade the version of aspose_words? Please help.

Looks like you need to experiment with that, with some debugging logging.

I have a PDF to word converter. I'm using the library aspose_words version 24.10.0. The Python Version is 3.10.5. The converter is not working in PythonAnywhere even though it's working in my local machine. From what I've checked, aspose-words is supposed to work with Python >= 3.6 to < 3.12. It's probably a PythonAnywhere compatibility issue. Please help.

How are you running it and what error do you see?

The following messages are generated in the server log. But there is no error log for the 502 error in the browser.

2024-11-12 00:55:58 Tue Nov 12 00:55:57 2024 - *** HARAKIRI ON WORKER 2 (pid: 17, try: 1) ***
2024-11-12 00:55:58 Tue Nov 12 00:55:57 2024 - HARAKIRI !!! worker 2 status !!!
2024-11-12 00:55:58 Tue Nov 12 00:55:57 2024 - HARAKIRI [core 0] 10.0.5.105 - POST /pdftoword since 1731367256
2024-11-12 00:55:58 Tue Nov 12 00:55:57 2024 - HARAKIRI !!! end of worker 2 status !!!
2024-11-12 00:55:58 DAMN ! worker 2 (pid: 17) died, killed by signal 9 :( trying respawn ...
2024-11-12 00:55:58 Respawned uWSGI worker 2 (new pid: 29)
2024-11-12 00:55:58 spawned 2 offload threads for uWSGI worker 2

[edit by admin: formatting]

A "HARAKIRI" message would normally mean that the worker process took more than five minutes to return a response, However, the "killed by signal 11" message in your earlier post suggests that the Python interpreter is crashing, which might mean that the version of aspose_words was compiled against a different version of Python to the one we have installed. How did you install aspose_words?

I installed aspose_words through requirements.txt. From what I've checked online, aspose_words is supposed to work with Python >= 3.6 to < 3.12. I'm using aspose_words version 24.10.0. The Python Version in PythonAnywhere is 3.10.5. What version of aspose_words should be used? Can you help with the specific version of aspose_words I need when I'm in PythonAnywhere?

You said the following line is failing:

doc = aw.Document(upload_folder + "/{}".format(f.filename)) where aw represents aspose.words

Is this still the case? Does your app work if you remove that part of the code? If so, what is the error? You might need to add additional logging to print out the error.