将 HTTP 请求发送到托管在 AWS EC2 实例上的服务器
Send HTTP request to server hosted on AWS EC2 instance
我正在尝试从客户端向托管在我的 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地址是正确的。我做错了什么?
您只能绑定到 EC2 实例的私有 IP 地址。 EC2 实例上的 IP 堆栈不知道 public IP,因为该地址由 VPC Internet 网关自动透明地转换 to/from 实例的私有 IP 地址。
绑定到私有地址或本库接受的任何内容 "any/all IPv4"。通常这是 0.0.0.0
,有时是 *
,但在这种情况下,BaseHTTPServer
需要一个空字符串。
server_address = ('', 9000)
出于某种原因更改为 server_address = ('0.0.0.0', 9000)
引发了类似的错误。
但改为 server_address = (' ', 9000)
有效。
我正在尝试从客户端向托管在我的 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地址是正确的。我做错了什么?
您只能绑定到 EC2 实例的私有 IP 地址。 EC2 实例上的 IP 堆栈不知道 public IP,因为该地址由 VPC Internet 网关自动透明地转换 to/from 实例的私有 IP 地址。
绑定到私有地址或本库接受的任何内容 "any/all IPv4"。通常这是 0.0.0.0
,有时是 *
,但在这种情况下,BaseHTTPServer
需要一个空字符串。
server_address = ('', 9000)
出于某种原因更改为 server_address = ('0.0.0.0', 9000)
引发了类似的错误。
但改为 server_address = (' ', 9000)
有效。