简体   繁体   English

服务器到Amazon EC2实例与python通信?

[英]Server to amazon EC2 instance communication with python?

I have this. 我有这个。 But it only works locally. 但这仅在本地有效。 I always receive a connection timeout when I run the client. 运行客户端时,我总是收到连接超时。 The port on the server is open to the default security group. 服务器上的端口对默认安全组开放。

server.py: server.py:

import SocketServer

class MyTCPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        self.data = self.request.recv(1024).strip()
        print self.client_address
        print self.data
        self.request.send(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "", 9800
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
    server.serve_forever()

client.py: client.py:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto('Hello, world\n'('host.ip',  9800))
data = s.recv(1024)
s.close()

在客户端上,您使用的是UDP的 socket.SOCK_DGRAM ,但是您在使用TCP服务器(应该是socket.SOCK_STREAM )。

看起来您正在打开端口9800,但正在与端口9999通讯

The code works correctly. 该代码正常工作。

Within the AWS console within the Networking and Security tab select Security Groups and within the default security profile under the 'inbound' tab - add your port to the list... 在`` 网络和安全性''选项卡的AWS控制台中,选择`` 安全组 '',然后在``入站''选项卡下的默认安全配置文件中-将端口添加到列表...

using a source of 0.0.0.0/0 will listen on all ports. 使用0.0.0.0/0将侦听所有端口。

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

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