2012-12-07 53 views
2

無法使用具有公共IP地址的EC2 Ubuntu Linux 12.04實例啓動bottle.py服務器。程序(hw.py)來自瓶子教程即。EC2,bottle.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 

更改爲:運行(主機= 'xxx.xxx.xxx.xxx',端口= 8080)

該錯誤是socket.error:[錯誤99]無法分配請求的地址。

更改爲:運行(主機= '0.0.0.0',端口= 8080)

...結合所述服務器到所有IP /接口。將EC2公共IP地址輸入到帶有或不帶有8080的瀏覽器中將顯示「此頁面不可用」。

理想情況下,第一個選項,即。運行(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)       = ? 
+0

你有沒有嘗試過以root身份運行Python? – Blender

+0

> sudo python hw.py給出了無法分配請求的地址錯誤。 –

+0

使用'strace -f'運行,在出錯前粘貼相關位。在錯誤發生之前, – soulseekah

回答

5

試試這個。我用它來測試網絡。在服務器的後臺

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 & 

那麼我認爲你現在可以訪問。


Changing to: run(host='0.0.0.0', port=8080)

... which binds the server to all IPs/interfaces. Typing the EC2 public IP address into browser with and without :8080 displays a "This page is not available".

情況下,該錯誤的,我認爲你需要在你的根URL的一端與/hello參觀。

+0

試過但沒有工作。首先,使用公共IP地址(xxx.xxx.xxx.xxx)與EC2的連接確實起作用,因爲我正在使用另一個Python Web服務器進行測試並且已連接。但是,bottle.py不起作用。也試着用xxx.xxx.xxx.xxx:8080/hello,那也不管用。試過Chrome和FF - 沒有運氣! –

+1

@dbv你可以試試'$ ps aux | grep hw.py'來檢查它是否正在運行。而b.t.w,你確定你已經爲你的服務開通了港口嗎? – Kjuly

+0

通過EC2打開了0.0.0.0:8080端口,現在可以工作。謝謝!順便說一句,你知道爲什麼bottle.py不綁定在端口80的EC2公共IP地址在hw.py即ie。 xxx.xxx.xxx.xxx:80?此外,當標準瓶服務器被替換爲多線程服務器時,這個限制是否適用? –