简体   繁体   English

将HTTP请求发送到托管在AWS EC2实例上的服务器

[英]Send HTTP request to server hosted on AWS EC2 instance

I'm trying to send an HTTP POST request from the client to a server hosted on my AWS EC2 instance. 我正在尝试从客户端向我的AWS EC2实例上托管的服务器发送HTTP POST请求。 I get this error when I try to set up the server on AWS. 尝试在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

Here's my code: 这是我的代码:

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)

I've confirmed that the IP address is correct. 我已经确认IP地址正确。 What am I doing wrong? 我究竟做错了什么?

You can only bind to the private IP address of an EC2 instance. 您只能绑定到EC2实例的私有IP地址。 The IP stack on an EC2 instance is unaware of the public IP, because that address is automatically and transparently translated to/from the instance's private IP address by the VPC Internet Gateway. EC2实例上的IP堆栈不知道公共IP,因为该地址已由VPC Internet网关自动透明地转换为实例的专用IP地址或从该实例的专用IP地址透明地转换。

Bind to the private address or whatever this library accepts as "any/all IPv4". 绑定到私有地址或此库接受为“任何/所有IPv4”的任何地址。 Typically this is 0.0.0.0 , sometimes * , but in this case, BaseHTTPServer expects an empty string. 通常为0.0.0.0 ,有时为* ,但是在这种情况下, BaseHTTPServer需要一个空字符串。

server_address = ('', 9000) 

For some reason changing to server_address = ('0.0.0.0', 9000) threw a similar error. 由于某种原因,更改为server_address = ('0.0.0.0', 9000)引发类似的错误。

But changing to server_address = (' ', 9000) worked. 但是更改为server_address = (' ', 9000)可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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