1

I want to send webcam video from my laptop to aws EC2 instance.

I'm trying to follow suggestions from here and code from here.

The issue I'm facing is that I do not know how to open a socket and listen to incoming traffic on EC2. My EC2 is a Amazon Linux free tier instance. No matter what I try I can't get it to work.

I added an inbound rule to allow TCP traffic on the port I want to listen to.

If it helps, binding to a port doesn't seem to be an issue but it seems that the code gets stuck on socket.accept() line of code from link 2.

I would appreciate if somebody showed me how to do this properly.

mirzaD14
  • 13
  • 4

2 Answers2

0

Try this as a test (code is from here):

Run on your server (adjusting IP and the port to match your ec2):

#!/usr/bin/env python

import socket


TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 20  # Normally 1024, but we want fast response

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

conn, addr = s.accept()
print 'Connection address:', addr
while 1:
    data = conn.recv(BUFFER_SIZE)
    if not data: break
    print "received data:", data
    conn.send(data)  # echo
conn.close() 

And try to send data from another machine using this (adjust IP to match your EC2 public IP) :

#!/usr/bin/env python

import socket


TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()

print "received data:", data

If that doesn't work, then post more details (screen shots of your security setup or similar).

Fraggle
  • 61
  • 1
  • 2
  • 10
  • Thanks for the comment @Fraggle. My issue was in the client script. I did not know that s.accept() just stops everything and waits for connection from outside. Since the client script was not sending the data to the right address, server script was always waiting for connection to come. – mirzaD14 Apr 27 '21 at 10:41
0

If it's stuck in socket.accept() it means it's waiting for connection. In the client script what IP are you connecting to? It must not be localhost, nor the instance private IP (e.g. 172.31.x.x) but instead its Public IP address - you can find it in the EC2 details. Also note that the Security Group must be open from your laptop's public IP (or from all IPs but that's far less secure).

MLu
  • 23,798
  • 5
  • 54
  • 81