Server to amazon EC2 instance communication with python?
-
12-03-2021 - |
سؤال
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()
المحلول
On client you're using socket.SOCK_DGRAM
which is UDP, yet you are using TCP server (which would be socket.SOCK_STREAM
).
نصائح أخرى
Looks like you're opening port 9800 but talking to port 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.
لا تنتمي إلى StackOverflow