简体   繁体   中英

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:

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:

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...

using a source of 0.0.0.0/0 will listen on all ports.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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