【问题标题】:EC2, bottle.py connectionEC2、bottle.py 连接
【发布时间】:2012-11-25 10:56:04
【问题描述】:

无法使用具有公共 IP 地址的 EC2 Ubuntu Linux 12.04 实例启动 bottle.py 服务器。程序(hw.py)来自瓶子教程,即。

from bottle import route, run

@route('/hello')
def hello():
    return 'hello world'

run(host='xxx.xxx.xxx.xxx', port=80)

运行方式:

> python hw.py
or
> sudo python hw.py

产生错误:

Traceback (most recent call last):
  File "hw.py", line 7, in <module>
    run(host='xxx.xxx.xxx.xxx', port=80)
  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 2700, in run
    server.run(app)
  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 2382, in run
    srv = make_server(self.host, self.port, handler, **self.options)
  File "/usr/lib/python2.7/wsgiref/simple_server.py", line 144, in make_server
    server = server_class((host, port), handler_class)
  File "/usr/lib/python2.7/SocketServer.py", line 408, in __init__
    self.server_bind()
  File "/usr/lib/python2.7/wsgiref/simple_server.py", line 48, in server_bind
    HTTPServer.server_bind(self)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
    SocketServer.TCPServer.server_bind(self)
  File "/usr/lib/python2.7/SocketServer.py", line 419, in server_bind
    self.socket.bind(self.server_address)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address

EC2 公共 IP 地址未附加到任何其他进程。端口 80 未附加到另一个进程。

> sudo netstat -lp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 *:ssh                   *:*                     LISTEN      629/sshd
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      629/sshd
udp        0      0 *:bootpc                *:*                                 463/dhclient3
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     7190     667/dbus-daemon     /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     5888     1/init              @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     7312     741/acpid           /var/run/acpid.socket
unix  2      [ ACC ]     SEQPACKET  LISTENING     26855    21843/udevd         /run/udev/control

改为:run(host='xxx.xxx.xxx.xxx', port=8080)

错误是 socket.error: [Errno 99] Cannot assign requested address.

改为:run(host='0.0.0.0', port=8080)

... 将服务器绑定到所有 IP/接口。在浏览器中输入 EC2 公共 IP 地址(无论是否使用 :8080)都会显示“此页面不可用”。

理想情况下,第一个选项即。 run(host='xxx.xxx.xxx.xxx', port=80) 应该可以工作,因为这就是其他 Web 服务器的启动方式。任何人都知道如何解决这个问题?

编辑:从 strace -f 添加最后一组行:

write(2, "  File \"/usr/lib/python2.7/Socke"..., 70  File "/usr/lib/python2.7/SocketServer.py", line 419, in server_bind
) = 70
open("/usr/lib/python2.7/SocketServer.py", O_RDONLY) = 4
fstat(4, {st_mode=S_IFREG|0644, st_size=23321, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f434cbfe000
read(4, "\"\"\"Generic socket server classes"..., 4096) = 4096
read(4, "service) is to maintain an\nexpli"..., 4096) = 4096
read(4, "set()\n\n    def shutdown(self):\n "..., 4096) = 4096
read(4, "n't use serve_forever()\n    - fi"..., 4096) = 4096
write(2, "    ", 4    )                     = 4
write(2, "self.socket.bind(self.server_add"..., 38self.socket.bind(self.server_address)
) = 38
close(4)                                = 0
munmap(0x7f434cbfe000, 4096)            = 0
write(2, "  File \"/usr/lib/python2.7/socke"..., 57  File "/usr/lib/python2.7/socket.py", line 224, in meth
) = 57
open("/usr/lib/python2.7/socket.py", O_RDONLY) = 4
fstat(4, {st_mode=S_IFREG|0644, st_size=20485, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f434cbfe000
read(4, "# Wrapper module for _socket, pr"..., 4096) = 4096
read(4, "oo long.\"\n    errorTab[10064] = "..., 4096) = 4096
write(2, "    ", 4    )                     = 4
write(2, "return getattr(self._sock,name)("..., 39return getattr(self._sock,name)(*args)
) = 39
close(4)                                = 0
munmap(0x7f434cbfe000, 4096)            = 0
write(2, "socket", 6socket)                   = 6
write(2, ".", 1.)                        = 1
write(2, "error", 5error)                    = 5
write(2, ": ", 2: )                       = 2
write(2, "[Errno 99] Cannot assign request"..., 42[Errno 99] Cannot assign requested address) = 42
write(2, "\n", 1
)                       = 1
rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7f434c7d1cb0}, {0x41d920, [], SA_RESTORER, 0x7f434c7d1cb0}, 8) = 0
close(3)                                = 0
exit_group(1)                           = ?

【问题讨论】:

  • 您是否尝试过以 root 身份运行 Python?
  • > sudo python hw.py 给出无法分配请求的地址错误。
  • 使用strace -f运行,在错误前粘贴相关位。
  • 在错误之前添加了最后一组行。谢谢。

标签: python ubuntu amazon-ec2 bottle


【解决方案1】:

试试这个。我用它来测试网络。

from bottle import Bottle, run

app = Bottle()

@app.route('/')
def index():
    return 'Service Running'

run(
        app,                    # Run |app| Bottle() instance
        host     = '0.0.0.0',
        port     = 8080,
        reloader = True,        # restarts the server every time edit a module file
        debug    = True         # Comment out it before deploy
        )

在您的服务器后台运行此文件 (hw.py):

$ cd path/to/your/file
$ nohup python server.py &

那我想你现在可以访问了。


改为:run(host='0.0.0.0', port=8080)

... 将服务器绑定到所有 IP/接口。在浏览器中输入 EC2 公共 IP 地址(无论是否使用 :8080)都会显示“此页面不可用”。

这个错误的情况,我认为你需要在你的根 URL 的末尾加上/hello 访问。

【讨论】:

  • 试过但没用。首先,使用公共 IP 地址 (xxx.xxx.xxx.xxx) 与 EC2 的连接确实有效,因为我正在使用另一个 Python Web 服务器进行测试并且该服务器已连接。但是,bottle.py 不起作用。还尝试使用 xxx.xxx.xxx.xxx:8080/hello 也不起作用。尝试了 Chrome 和 FF - 没有运气!
  • @dbv 您可以尝试$ ps aux | grep hw.py 来检查它是否正在运行。顺便说一句,你确定你已经为你的服务打开了端口吗?
  • 通过 EC2 打开了 0.0.0.0:8080 端口,现在它可以工作了。谢谢!顺便说一句,你知道为什么 bottle.py 不与 hw.py 中端口 80 的 EC2 公共 IP 地址绑定,即。 xxx.xxx.xxx.xxx:80?另外,当标准的瓶子服务器被替换为多线程服务器时,这个限制是否适用?
  • @dbv 我认为您需要在 AWS 控制台面板上打开端口 80。
  • 正在测试的其他 Web 服务器使用端口 80,因此已经打开。我的猜测是,更换标准瓶服务器后,ip:port 组合可用。
猜你喜欢
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 2016-02-12
  • 2017-03-20
  • 2015-08-29
  • 2016-04-23
  • 2018-10-31
  • 1970-01-01
相关资源
最近更新 更多