简体   繁体   中英

Can't run python sanic on ec2

I tried a code bellow on ec2.(amazon linux2)

from sanic import Sanic
from sanic.response import json

app = Sanic()

@app.route('/')
async def test(request):
    return json({'hello': 'world'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

It runs fine.

Bue when I changed a port number to 80 like bellow

from sanic import Sanic
from sanic.response import json

app = Sanic()

@app.route('/')
async def test(request):
    return json({'hello': 'world'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)

System replys,

main.py:4: DeprecationWarning: Sanic(name=None) is deprecated and None value support for `name` will be removed in the next release. Please use Sanic(name=‘your_application_name’) instead.
 app = Sanic()
[2020-06-16 08:16:15 +0000] [8303] [INFO] Goin’ Fast @ http://0.0.0.0:80
[2020-06-16 08:16:15 +0000] [8303] [ERROR] Unable to start server
Traceback (most recent call last):
 File “/home/ec2-user/sanic/lib64/python3.7/site-packages/sanic/server.py”, line 835, in serve
  http_server = loop.run_until_complete(server_coroutine)
 File “uvloop/loop.pyx”, line 1456, in uvloop.loop.Loop.run_until_complete
 File “uvloop/loop.pyx”, line 1727, in create_server
PermissionError: [Errno 13] error while attempting to bind on address (‘0.0.0.0’, 80): permission denied
[2020-06-16 08:16:15 +0000] [8303] [INFO] Server Stopped

So next I did

sudo python main.py

Then error message says,

File “main.py”, line 7
  async def test(request):
    ^
SyntaxError: invalid syntax

I don't know what to do. Give me some advise.

My folder is like below

sanic/
 ├ bin/
 ├ includs/
 ├ lib/
 ├ lib64
 ├ main.py 
 └ pyvenv.cfg

try to use:

sudo python3 main.py

Thankfully, I got a solution on my own. It caused by a PATH error,since I used venv.

when I use

sudo python3 main.py

then sys.path is like

['/home/ec2-user/sanic', '/usr/lib64/python37.zip', '/usr/lib64/python3.7', '/usr/lib64/python3.7/lib-dynload', '/usr/lib64/python3.7/site-packages'] There isn't a PATH that contains "sanic" or any module than I installed before.

So I added

import sys sys.path.append('/home/ec2-user/sanic/lib/python3.7/site-packages') to main.py

then It runs fine.

So, when you use "sudo" on amazon linux2 it will get out from venv.

I also checked

`sudo python main.py'

then it returns

File "main.py", line 15 async def heavy_task1(): ^ SyntaxError: invalid syntax

I believe there isn't any syntax error. I don't know why it happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM