我正在尝试从客户端向我的AWS EC2实例上托管的服务器发送HTTP POST请求。尝试在AWS上设置服务器时收到此错误。
Traceback (most recent call last):
File "testReceive_AWS.py", line 197, in <module>
httpd = server_class((server_address), MyHandler)
File "/usr/lib/python2.7/SocketServer.py", line 417, in __init__
self.server_bind()
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 431, in server_bind
self.socket.bind(self.server_address)
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address
这是我的代码:
if __name__ == '__main__':
server_class = BaseHTTPServer.HTTPServer
handler_class=BaseHTTPServer.BaseHTTPRequestHandler
server_address = ('EC2 instance public ip address', 9000)
httpd = server_class((server_address), MyHandler)
print time.asctime(), "Server Starts - %s:%s" % (server_address)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print time.asctime(), "Server Stops - %s:%s" % (server_address)
我已经确认IP地址正确。我在做什么错了?
答案 0 :(得分:2)
您只能绑定到EC2实例的专用IP地址。 EC2实例上的IP堆栈不知道公共IP,因为该地址由VPC Internet网关自动透明地转换为实例的私有IP地址或从该实例的私有IP地址透明地转换。
绑定到私有地址或该库接受为“任何/所有IPv4”的任何内容。通常,这是0.0.0.0
,有时是*
,但是在这种情况下,BaseHTTPServer
需要一个空字符串。
server_address = ('', 9000)
答案 1 :(得分:0)
由于某种原因,更改为server_address =('0.0.0.0',9000)时也会发生类似的错误,但是更改为server_address =('',9000)还是可以的。