Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
1,369 Views

Connecting to a socket on the Intel Edison module

Greetings.

How would I proceed if I want to connect to a socket which is on our backend from the Intel Edison module? Is it standard python socket scripting or is there any other way that I need to go?

Thanks!

0 Kudos
5 Replies
Highlighted
Community Manager
4 Views

Hi Zettez,

 

 

Normally in Transmission Control Protocol (TCP) is used with Netcat. Netcat is a simple Unix utility which reads and writes data across network connections, using TCP or UDP protocol (Check this link for more information: http://nc110.sourceforge.net/)

 

 

I personally recommend you to use it, check this link: https://linux.die.net/man/1/nc, it can be very helpful and you can learn how to use it. Try to use it first in your computer with Linux and then try to use it in your Edison. This package should be already included in your Edison Image.

 

 

I hope you find this information useful.

 

 

Regards,

 

-Leonardo
0 Kudos
Highlighted
New Contributor I
4 Views

Hey!

Thanks, I will look into it. I need to do this in some scripts, maybe I can invoke Netcat from within my script.

0 Kudos
Highlighted
Community Manager
4 Views

Hi Zettez,

 

 

Great! Please let us know if it works.

 

 

And don't doubt to ask us if you have issues.

 

 

Regards,

 

-Leonardo
0 Kudos
Highlighted
New Contributor I
4 Views

Hey!

I successfully connected to our socket by installing websocket-client by typing pip install websocket-client and using this python code (not that pretty but it works) (indentation was scrambled when I copy pasted):

# !/usr/bin/python

import os

import time

import json

from websocket import create_connection

def handleAction(action, args):

print(action)

# Do something with the action

while True:

time.sleep(8)

try:

print("create connection to socket")

socket = create_connection("ws://111.222.333.444:555")

try:

while True:

print("reading data..")

result = socket.recv()

print("got data: ")

print(result)

print(type(result))

data = json.loads(result)

action = data["action"]

args = data["args"]

handleAction(action, args)

except Exception as error:

print("socket receive error")

print(error)

socket.close()

except Exception as error:

print("socket error")

print(error)

socket.close()

0 Kudos
Highlighted
Community Manager
4 Views

Hi Zettez,

 

 

That's awesome, we really appreciate that you shared the script.

 

 

It will be helpful for another users.

 

 

Thanks.

 

 

Regards,

 

-Leonardo
0 Kudos