Hasseb USB DALI Master & python library python-dali - python

We have Hasseb USB Dali Master and try to used python library (github.com/sde1000/python-dali). But codes from section “Examples” don’t work.
We used Armbian Linux (Ubuntu based operating system)
The library https://github.com/onitake/daliserver was installed.
We try to start set_single.py separately.
$sudo python set_single.py
Traceback (most recent call last):
File "set_single.py", line 25, in <module> d.send(cmd)
File "build/bdist.linux-armv7l/egg/dali/driver/daliserver.py", line 43, in send
File "/usr/lib/python2.7/socket.py", line 575, in create_connection raise err
socket.error: [Errno 111] Connection refused
And try to start dalliserver in another terminal.
$ sudo python server.py
('Connection address_:', ('127.0.0.1', 43653))
Traceback (most recent call last):
File "server.py", line 14, in <module>
s.bind((TCP_IP, TCP_PORT))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
(This error occurred when set_single.py started)
$ sudo python set_single.py
Traceback (most recent call last):
File "set_single.py", line 25, in <module>
d.send(cmd)
File "build/bdist.linux-armv7l/egg/dali/driver/daliserver.py", line 66, in send
File "build/bdist.linux-armv7l/egg/dali/driver/daliserver.py", line 84, in unpack_response
struct.error: unpack requires a string argument of length 4
file set_single.py
from dali.address import Broadcast
from dali.address import Short
from dali.gear.general import DAPC
from dali.driver.daliserver import DaliServer
import sys
if __name__ == "__main__":
addr = Broadcast()
level = int(150)
d = DaliServer("localhost", 55825 )
cmd = DAPC(addr, level)
d.send(cmd)
file server.py
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 55825
BUFFER_SIZE = 20
while True:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
try:
print("Connection address_:", addr)
while 1:
conn.setblocking(0)
conn.settimeout(20.0)
data = conn.recv(BUFFER_SIZE)
if not data:
break
stream = ":".join("{:02x}".format(ord(chr(c))) for c in data)
print("received data: [{1}] {0}".format(stream, len(data)))
conn.send(b"\x02\xff\x00\x00")
except:
pass
conn.close()

Related

Why am I getting a "Connection refused" in python? Can't resolve using existing solutions

import ssl
import socket
port = 3001
while True:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ssl_socket = ssl.wrap_socket(s, cert_reqs=ssl.CERT_REQUIRED, ca_certs='scert.pem')
ssl_socket.connect(('127.0.0.1', 3001))
ssl_socket.write(str(input("Enter two numbers to add:")).encode())
z = ssl_socket.recv(1024)
print(z.decode())
ssl_socket.close()
The error I'm getting is:
Traceback (most recent call last):
ssl_socket.connect(('127.0.0.1', 3001))
self._real_connect(addr, False)
socket.connect(self, addr)
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 111] Connection refused
I am confused as to what to do. Already tried the methods on the other thread on socket.

Traceback Error in Python Code when runs of Ubuntu

I wrote this code that functions properly on Windows but gives a few errors on my Ubuntu 12.04. Although the code performs well its intended function but its given some errors which I don't want.. Kindly help me in this regard..
from socket import *
from threading import Thread
from Crypto.Cipher import AES
import os
import base64
import timeit
# Receiveing + Decoding the Information, symmetrical key isi
def clientHandler():
conn, addr = s.accept()
print addr, "is connected"
while 1:
data = conn.recv(1024)
if not data:
break
print "Metering Data Received: Processing..."
#creating decoding unpadding
PADDING ="{"
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
#creating a default key
obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
decrypted_data = DecodeAES(obj2,data)
print decrypted_data
HOST = "" #localhost
PORT = 12000
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(4)
print "Server is runnig"
#Thread(target=clientHandler).start()
#Thread(target=clientHandler).start()
#Thread(target=clientHandler).start()
for i in range(4):
Thread(target=clientHandler).start()
s.close()
And this is what appears on the terminal of Ubuntu but not on Windows based...
Server is runnig
Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "chat_encrypt.py", line 10, in clientHandler
conn, addr = s.accept()
File "/usr/lib/python2.7/socket.py", line 202, in accept
sock, addr = self._sock.accept()
File "/usr/lib/python2.7/socket.py", line 170, in _dummy
raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor
for i in range(4):
Thread(target=clientHandler).start()
s.close()
The last line closes the socket ... which each thread then tries to 'accept'.

Unable to connect to socket using SSL and python.

My code
I've gone over it and had a friend of mine look at it. At this point we're both stumped and would appreciate some help.
import os
import socket
import ssl
HSM = raw_input('Please enter the IP address of the machine you are connecting to.')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sslSocket = ssl.wrap_socket(sock,
keyfile="AKMClientPrivateKey.pem",
certfile="AKMClientSignedCert.pem",
ca_certs="TCASelfSignedCert.pem",
cert_reqs=ssl.CERT_REQUIRED)
sslSocket.connect(('192.168.160.128', 6000))
print "Connection is successful!"
The error I keep getting
I have all of the keys and certs I'm referencing in the same folder as my python file.
I've double checked the IP address and port.
Traceback (most recent call last):
File "connect.py", line 12, in <module>
sslSocket.connect(['192.168.160.128', 6000])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 333, in connect
self._real_connect(addr, False)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 314, in _real_connect
self.ca_certs, self.ciphers)
ssl.SSLError: [Errno 336265218] _ssl.c:351: error:140B0002:SSL
routines:SSL_CTX_use_PrivateKey_file:system lib

Python socket (Socket Error Bad File Descriptor)

The following receiveFile() function reads a filename and file data from the socket and splits it using the delimiter $.
But I am unable to close the socket and a Bad file descriptor error is raised. If I comment out the self.server_socket.close() statement then there is no error but the socket is listening forever.
Code:-
def listen(self):
self.server_socket.listen(10)
while True:
client_socket, address = self.server_socket.accept()
print 'connected to', address
self.receiveFile(client_socket)
def receiveFile(self,sock):
data = sock.recv(1024)
data = data.split("$");
print 'filename', data[0]
f = open(data[0], "wb")
#data = sock.recv(1024)
print 'the data is', data[1]
f.write(data[1])
data = sock.recv(1024)
while (data):
f.write(data)
data=sock.recv(1024)
f.close()
self.server_socket.close()
print 'the data is', data
print "File Downloaded"
Traceback:-
Traceback (most recent call last):
File "server.py", line 45, in <module>
a = Server(1111)
File "server.py", line 15, in __init__
self.listen()
File "server.py", line 20, in listen
client_socket, address = self.server_socket.accept()
File "c:\Python27\lib\socket.py", line 202, in accept
sock, addr = self._sock.accept()
File "c:\Python27\lib\socket.py", line 170, in _dummy
raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor
You are closing the server's listening socket, and after that calling again accept() on it.
To finish receiving one file you should close client connection's socket (sock in function receiveFile).
in this code i am trying to shut down the server once file is received
What you'll need is something to break out of the while True loop when you want to shut down the server. A simple solution would be to exploit the exception generated when you close the server socket...
def listen(self):
self.server_socket.listen(10)
while True:
try:
client_socket, address = self.server_socket.accept()
except socket.error:
break
print 'connected to', address
self.receiveFile(client_socket)
print 'shutting down'

how to handle socket errors as exceptions in python + paramiko?

I want to return an error code when the following error gets raised:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "UserManagementRemote.py", line 202, in create_group
ssh.connect(hostname, username=user, password=remotepass)
File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 290, in connect
sock.connect(addr)
File "<string>", line 1, in connect
socket.error: [Errno 113] No route to host
>>>
But I'm currently having trouble catching the error raised.
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=user, password=remotepass)
except paramiko.AuthenticationException:
return 259
except socket.error:
return 261
chan = ssh.get_transport().open_session()
chan.exec_command(command)
codest = chan.recv_exit_status()
ssh.close()
return codest
Resulting on this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "UserManagementRemote.py", line 207, in create_group
except socket.error:
NameError: global name 'socket' is not defined
>>>
Any ideas?
Do
import socket
in the module where you do the exception handling.
To prevent this problem in the future, run pyflakes on all your source files. That will catch a lot of other errors as well.

Categories