Forums

My app not parsing some lines properly when running on pythonanywhere while it works locally

I was using a python library that had a bug for parsing some lines of a file, a new library version has been released fixing it. I´ve upgraded that on pythonanywhere, but it still not working, have then deleted all my folders/files, created all folders/files and install the python library (new version) from scratch, but my app still not parsing some lines needed yet.

However the same app runs without issues locally, have compared my code on pythonanywhere against that one OK running locally and it is the same, the only change between them is the path (UPLOAD_FOLDER). PythonAnywhere is also running the new version of the Python Library.

Any ideas what that could be ?

Thank you !

what is the library you are using that had a bug? and what are you trying to parse?

the python library is ciscoconfparse. I am trying to parse a cisco full config file into a basic file (removing the management lines). The banner block lines are not being removed from the full config file in PythonAnywhere while it does when running locally. My PythonAnywhere profile has that same most up to date python library and also the same code lines than the ones I am running locally.

hmm you wouldn't be running windows locally or anything right? (just want to make sure it's not something stupid wrt line breaks)

are you running this in your console or on a webapp?

yeah, That runs OK locally using two machines (Win 7 and 8). In PtAnywhere I am running it as a webapp, I´ve just tried to test it under PTAnywhere Bash console, but get the error below, have deleted the webapp as well, but it still, not sure if I am able to run it under the bash.

fabianomota@giles-liveconsole2:~/mysite$ python app.py
 * Running on http://0.0.0.0:5000/
Traceback (most recent call last):
  File "app.py", line 148, in <module>
    app.run (port=5000, debug=True, host='0.0.0.0')
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 739, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 613, in run_simple
    test_socket.bind((hostname, port))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

[edit by admin: formatting]

so one possibility is the fact that linux and windows have different line endings. which may be causing confusion for your webapp.

(eg: if you uploaded a windows file but it was expecting linux, so it wasn't able to process it)

yeah, that seems to be the issue, have tested it locally running Ubuntu and the result is the same as in PTAnywhere, the banner lines block from the .txt file are not being removed while it does when running under Win.

Any idea how can get this solved, it would be something on the python library or unicode in my web app?

Thank you !

Hmm, just jumping in here while Conrad's away from the weekend -- one thing I should make clear is why things failed in your post when you tried to run the script from the command line (great-grandparent of this post) -- you were doing an app.run in a Flask app inside PythonAnywhere, which tries to create a socket server on our servers, which won't work.

But anyway, it does sound like the real problem is that there's an issue with the line endings. Can you not just convert the text file to use Unix line endings? Or is it being loaded by code that's outside your control?

Thank you Conrad and Giles, have resolved that opening the file in my python web app as universal line-endings mode: open(filename, 'rU').

If the problem comes from ending lines, just use Python to read the file line by line. Python manages this very well. If you cant do that, just ook in the source of the methd of readline for example to see how Python manages the different endlines.

I'm getting the same kind of troubles as Fabian at the time when trying to reload my app within the virtualenv. I have double-checked I'm not running locally or anything on the same port (I've rebooted my pc twice already). Any idea how I can sort this out? Thank you in advance.

(myvirtualenv)15:41 ~/mysite $ ls
__pycache__  flask_app.py  flask_app.pyc  forms.py  forms.pyc  static  templates
(myvirtualenv)15:41 ~/mysite $ python flask_app.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Traceback (most recent call last):
  File "flask_app.py", line 905, in <module>
    app.run(debug=True)
  File "/home/pwa/.virtualenvs/myvirtualenv/local/lib/python2.7/site-packages/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)
  File "/home/pwa/.virtualenvs/myvirtualenv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 618, in run_simple
    test_socket.bind((hostname, port))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

[edit by admin: formatting]

On PythonAnywhere, you don't need to run your Flask app directly; our web-hosting infrastructure takes care of that, and runs a bunch of separate copies of your web app so that it can handle more than one request at once (and also handles static files efficiently, etc.)

So to reload it, you either should use the "Reload" button on the "Web" tab or, if you really need to do it from the command line, by touching the WSGI file -- ie. doing this from a bash console

touch /var/www/pwa_pythonanywhere_com_wsgi.py

(Changing the filename appropriately if it's a different web app you want to reload.)

The latter technique won't update static file settings and other things that you can control from the web tab, and it does take up to ten seconds to have an effect, but if you just want it to reload your source code, it should be sufficient.

Thank you! It works without any problems!!. I haven't realised it was that simple (I really thought I had to run the app from a virtualenv console).

No problem, glad to help :-)