jczic / MicroWebSrv2

The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
https://github.com/jczic/MicroWebSrv2
MIT License
662 stars 97 forks source link

Permission error after running it right out of the box #35

Open jdcrunchman opened 4 years ago

jdcrunchman commented 4 years ago
   ---------------------------
   - Python pkg MicroWebSrv2 -
   -      version 2.0.6      -
   -     by JC`zic & HC2     -
   ---------------------------

MWS2-INFO> Stops the managed pool. Traceback (most recent call last): File "/Users/johndraper/Documents/Projects/WhisperMagic/MicroWebSrv2-master/MicroWebSrv2/libs/XAsyncSockets.py", line 375, in Create srvSocket.bind(srvAddr) PermissionError: [Errno 13] Permission denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/johndraper/Documents/Projects/WhisperMagic/MicroWebSrv2-master/MicroWebSrv2/microWebSrv2.py", line 191, in StartInPool bufSlots = xBufSlots ) File "/Users/johndraper/Documents/Projects/WhisperMagic/MicroWebSrv2-master/MicroWebSrv2/libs/XAsyncSockets.py", line 378, in Create raise XAsyncTCPServerException('Create : Error to binding the TCP server on this address.') MicroWebSrv2.libs.XAsyncSockets.XAsyncTCPServerException: Create : Error to binding the TCP server on this address.

I downloaded it, UnZipped it, and without any modifications to the file, this is what I get. Can someone please tell me what I have to do to deal with this error.

Should my "main.py" module be set for executable.

DaveHOnCode commented 4 years ago

in Linux Systems, you must start applications, which want bind to ports up to 10xx (don't know exactly) with admin rights

jdcrunchman commented 4 years ago

I'm running on a Mac 10.13.6 - Why is this happening. Is there any workaround. Could it be that this is a 32 bit only Mac... this version is an "in between" version from 32 bit to 64 bit. Most importantly is there a work around.

jdcrunchman commented 4 years ago

I'm trying to use the VSCode source level debugger to follow through the code. But the VSCode "run" command is not setup to run as root. So I tried to start VSCode from the shell command using sudo, but once I did that, the VSCode "broke" and now will ONLY run under root. I'm still trying to unravel the clusterfuck of bad things it did to my VSCode.

Why can't someone post a fix for MicroWebSrv2 release that will allow us to use the source level debugger without damaging our copy of VSCode.... PLEASE PLEASE HELP ME!!!!

jczic commented 4 years ago

You must run Visual Studio with administrator rights or set the MWS2 server port greater than 1024 (ex: 12345) : mws2.BindAddress = ('0.0.0.0', 12345) and open your local browser to http://localhost:12345

jdcrunchman commented 4 years ago

Then why didn't you mention that in the readme? Can you tell me where in the code you set the port/ip tuples? What routine, what line number please?

John

On Tue, Apr 7, 2020 at 7:20 AM Jean-Christophe Bos notifications@github.com wrote:

You must to run Visual Studio with administrator rights or set a MWS2 server port greater than 1024 (ex: 12345) : mws2.BindAddress = ('0.0.0.0', 12345) and open your local browser to http://localhost:12345

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jczic/MicroWebSrv2/issues/35#issuecomment-610415310, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAX4GOKQIF6YKS442STN5YDRLMZEHANCNFSM4L4TG43A .

jdcrunchman commented 4 years ago

I tried as you suggested. On line 89 of MicroWebSrv.py I changed it to: self._bindAddr = ('127.0.0.1', 12345)

On my Browser: I get... [1:38:27 PM] Connection to ws://127.0.0.1/wschat... [1:38:27 PM] [CONNECTION ERROR] [1:38:27 PM] [CONNECTION CLOSED]

I noticed:

in wschat.html, line 43 in javascript, does not seem to provide the port number. function init() { var scheme if (window.location.protocol == 'https:') scheme = 'wss:'; else scheme = 'ws:'; var wsUri = scheme + '//' + window.location.hostname + '/wschat'; writeLineToChat("Connection to " + wsUri + "...") websocket = new WebSocket(wsUri); websocket.onopen = function(evt) { onOpen (evt) }; websocket.onclose = function(evt) { onClose (evt) }; websocket.onmessage = function(evt) { onMessage (evt) }; websocket.onerror = function(evt) { onError (evt) }; getElmById("input-chat").addEventListener("keyup", onChatLine); getElmById("input-chat").focus() }

So, in line 50: I added this...

var wsUri = scheme + '//' + window.location.hostname + ':12345/wschat';

where I added the port number... that did NOT work, so I still cant get the wschat.html to work.

Every now and then, this is what I get in the shell window after starting it under the source level debugger, perhaps this might relate to the problem....

└─ $ ▶ env DEBUGPY_LAUNCHER_PORT=53603 /Users/johndraper/anaconda3/bin/python /Users/johndraper/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/wheels/debugpy/launcher /Users/johndraper/Documents/Projects/WhisperMagic/MWS-Internal_dr/main.py

   ---------------------------
   - Python pkg MicroWebSrv2 -
   -      version 2.0.6      -
   -     by JC`zic & HC2     -
   ---------------------------

MWS2-INFO> Server listening on 127.0.0.1:12345. MWS2-INFO> Starts the managed pool to wait for I/O events. bash: __git_ps1: command not found (base) johndraper @ Johns-MacBook-Pro-5 ~/Documents/Projects/WhisperMagic/MWS-Internal_dr └─ $ ▶ env DEBUGPY_LAUNCHER_PORT=55617 /Users/johndraper/anaconda3/bin/python /Users/johndraper/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/wheels/debugpy/launcher /Users/johndraper/Documents/Projects/WhisperMagic/MWS-Internal_dr/www/wschat.html Traceback (most recent call last): File "/Users/johndraper/anaconda3/lib/python3.7/runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "/Users/johndraper/anaconda3/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/Users/johndraper/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/wheels/debugpy/main.py", line 45, in cli.main() File "/Users/johndraper/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/wheels/debugpy/../debugpy/server/cli.py", line 429, in main run() File "/Users/johndraper/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/wheels/debugpy/../debugpy/server/cli.py", line 266, in run_file runpy.run_path(options.target, run_name=compat.force_str("main")) File "/Users/johndraper/anaconda3/lib/python3.7/runpy.py", line 261, in run_path code, fname = _get_code_from_file(run_name, path_name) File "/Users/johndraper/anaconda3/lib/python3.7/runpy.py", line 236, in _get_code_from_file code = compile(f.read(), fname, 'exec') File "/Users/johndraper/Documents/Projects/WhisperMagic/MWS-Internal_dr/www/wschat.html", line 1 <!DOCTYPE html> ^ SyntaxError: invalid syntax bash: __git_ps1: command not found (base) johndraper @ Johns-MacBook-Pro-5 ~/Documents/Projects/WhisperMagic/MWS-Internal_dr └─ $ ▶

This seemed to have referenced a version of Python 3.7 that I installed over a year ago, when playing around with Anaconda. I think something is referencing these old files somehow.

I had several versions of python, so I switched to another one.

Seemed to have not given me that: bash: __git_ps1: command not found anymore.

So in conclusion.. the ONLY place where I can change the ip/port tuple was line 89 of MicroWebSrv.py.

Is that where I should change it? This is what I get in the shell when I run it now...

Notice that VSCode is picking up the proper version of Python

└─ $ ▶ env DEBUGPY_LAUNCHER_PORT=51099 /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/johndraper/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/wheels/debugpy/launcher /Users/johndraper/Documents/Projects/WhisperMagic/MWS-Internal_dr/main.py

   ---------------------------
   - Python pkg MicroWebSrv2 -
   -      version 2.0.6      -
   -     by JC`zic & HC2     -
   ---------------------------

MWS2-INFO> Server listening on 127.0.0.1:12345. <---- so it appears to be listening it here. MWS2-INFO> Starts the managed pool to wait for I/O events.

Next I checked the browser.... and went into the WebSocket test page, I get this...

Connecting to ws://127.0.0.1...

ERROR : undefined

-- DISCONNECTED --

The Javascript code in wstest.html is this...

function testWebSocket() { var scheme if (window.location.protocol == 'https:') scheme = 'wss:'; else scheme = 'ws:'; var wsUri = scheme + '//' + window.location.hostname; writeToScreen("Connecting to " + wsUri + "...") websocket = new WebSocket(wsUri); websocket.onopen = function(evt) { onOpen (evt) }; websocket.onclose = function(evt) { onClose (evt) }; websocket.onmessage = function(evt) { onMessage (evt) }; websocket.onerror = function(evt) { onError (evt) }; }

Again, I noticed there is no specification of port number on this one either.

So I changed line 28 of wstest.html to:
var wsUri = scheme + '//' + window.location.hostname + ':12345'

Where I specified the port number.

Should I change the javascript "init" function to point to the port number? Remember, I cannot run this under root, not without seriously damaging all the internal files used my VSCode.

What else can I try?

ALSO, VERY STRANGE, but out of the scope of MicroWebSrv2, the very first time I run it under the source level debugger, I get....

└─ $ ▶ env DEBUGPY_LAUNCHER_PORT=55582 /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/johndraper/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/wheels/debugpy/launcher /Users/johndraper/Documents/Projects/WhisperMagic/MWS-Internal_dr/www/wstest.html Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/Users/johndraper/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/wheels/debugpy/main.py", line 45, in cli.main() File "/Users/johndraper/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/wheels/debugpy/../debugpy/server/cli.py", line 429, in main run() File "/Users/johndraper/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/wheels/debugpy/../debugpy/server/cli.py", line 266, in run_file runpy.run_path(options.target, run_name=compat.force_str("main")) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 261, in run_path code, fname = _get_code_from_file(run_name, path_name) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 236, in _get_code_from_file code = compile(f.read(), fname, 'exec') File "/Users/johndraper/Documents/Projects/WhisperMagic/MWS-Internal_dr/www/wstest.html", line 1 <!DOCTYPE html> ^

But if I put a breakpoint at line 70 in main.py just before I call....

mws2.StartManaged()

then have it continue on, it seems not to generate that error that shows up in my shell output. I know you probably are not familiar with WSCode, but this has been eating me for months.

Does anyone else have any ideas.? Is anyone else using VSCode under debugger?

John

jdcrunchman commented 4 years ago

My friend Andy reports his MWS2 is working just fine out of the box. And he's not running as root. We tried to see why I cannot open a port 80 socket.... and his worked, and mine DOESN'T - obviously this is NOT a MWS2 issue, but this issue I'm having is very perplexing. Is there anyone else out there who can help me.... Andys machine is NOT running as root. And yet his machine it works. WTF? I really respect an opportunity for you Jean to have a Zoom session with me to help me find this problem....

John

jczic commented 4 years ago

And if you open a terminal (shell) on your MacOS, change directory to the MWS2 example folder and run python3 main.py?

jdcrunchman commented 4 years ago

There is a big problem with that. Starting and running VSCode from the command line with sudo in front of it, causes some serious damage to internal files used by VSCode... Best described here....

https://developercommunity.visualstudio.com/content/problem/57634/can-only-launch-visual-studio-from-command-line-wi.html

John

On Tue, Apr 7, 2020 at 7:20 AM Jean-Christophe Bos notifications@github.com wrote:

You must to run Visual Studio with administrator rights or set a MWS2 server port greater than 1024 (ex: 12345) : mws2.BindAddress = ('0.0.0.0', 12345) and open your local browser to http://localhost:12345

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jczic/MicroWebSrv2/issues/35#issuecomment-610415310, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAX4GOKQIF6YKS442STN5YDRLMZEHANCNFSM4L4TG43A .