Flask SocketIO disconnect on OS Date Change - date

I have a Web application that is using Flask-socketIO.
__init__.py
import flask
import flask_socketio
app = flask.Flask(__name__)
socketio = flask_socketio.SocketIO()
socketio.init_app(app, manage_session=False)
I have this in my manage.py
socketio.run(app, host='0.0.0.0', port=80)
I have this in my socket_IO.py
#WebApp.socketio.on('disconnect', namespace='/all')
def disconnect():
"""Socket disconnected."""
print('Disconnected')
If i now set the system clock ( Ubuntu 16.04 ) for example 1 day forward i get socket disconnect. I can set the clock backwards to whatever i want it does not effect the socket.
What i expect to happen is for the socket not to fire the disconnect function on a change in the system clock.
Does flask-socketIO use the system clock and because of that makes the socket disconnect if it's set forward and not backwards?
Thanks in advance

Related

PYCOM FiPy does not attach to the Cellular Network - LTE Italy

I have tried to connect to the internet using LTE but the network won't attach.
I want to determine the proper carrier on my Fipy, I did not get any response, the list was empty.
The comand AT+CEREG?' response OK.
The firmware version that I have is LR5.1.1.0-47510
Does someone have any idea on how I could manage to attach to the network? Any previous experiences Thank you in advance.
`>>> from network import LTE
>>> lte=LTE()
>>> lte.send_at_cmd("AT+SQNCTM=?")
''
>>>
I have been running all variations of the default test program with any success
from network import LTE
import time
import socket
lte = LTE()
lte.attach()
print("attaching..",end='')
while not lte.isattached():
time.sleep(0.25)
print('.',end='')
print(lte.send_at_cmd('AT!="fsm"')) # get the System FSM
print("attached!")
lte.connect()
print("connecting [##",end='')
while not lte.isconnected():
time.sleep(0.25)
print('#',end='')
#print(lte.send_at_cmd('AT!="showphy"'))
print(lte.send_at_cmd('AT!="fsm"'))
print("] connected!")

Modbus RTU on an Raspberry Pi 3b+ as an master

My task is to develop an automation system for testing the device.
I have at my disposal:
raspberry pi(3b+/4)
modbus modules: icp con m - 7051D, m - 7055D, m-7061D, and few others.
At first I want to understand, how I can implement modbus protocol on RPi, how does it work.
In order to do that, I've used pymodbus library on RPi, which was working as an master, and modbusMAT on an PC, for recieving and decoding instructions from RPi. They were connected via rs-485 - USB adapter.
I've written some simple code, for writing a coil.
The RPi is communciating with PC, but the problem is, that the output, doesn't make sense to me.
Here's the code:
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
client = ModbusClient(method = 'rtu', port='/dev/ttyUSB0', stopbits=1, bytesize = 8, parity='N', baurdrate = '9600', timeout=0.3)
connection = client.connect()
print (connection)
client.write_coils(1, 1, unit=0x01)
Screenshot from modbusMat console
The output:
[9C][84][8C][E4][FF]
[9C][08][8C][B4][FF]
[9C][84][8C][E4][FF]
[38][18][18][8C][CA]
The output is related to changes I've made parameters. From top to bottom:
address=1, values=1, unit=0x01
address=2, values=1, unit=0x01
address=1, values=2, unit=0x01
address=1, values=1, unit=0x02
How I understand is, that if I've set address to 1, then the addres byte on the console should be the same - [01], not [9C].
I've managed before to successfully send some instructions to the icp modules(turn on, turn off coil) from PC, via RS-485 but not from RPi.

Simulate a TCP tarpit

I want to simulate test cross-platform connection failures / timeouts, starting with blocking connect()s:
#!/usr/bin/python3
import socket
s = socket.socket()
endpoint = ('localhost', 28813)
s.bind((endpoint))
# listen for connections, accept 0 connections kept waiting (backlog)
# all other connect()s should block indefinitely
s.listen(0)
for i in range(1,1000):
c = socket.socket()
c.connect(endpoint)
# print number of successfully connected sockets
print(i)
On Linux, it prints "1" and hangs indefinitely (i.e. the behavior I want).
On Windows (Server 2012), it prints "1" and aborts with a ConnectionRefusedError.
On macOS, it prints all numbers from 1 to 128 and then hangs indefinitely.
Thus, I could accept the macOS ignores the backlog parameter and just connect enough sockets for clients to block on new connections.
How can I get Windows to also block connect() attempts?
On Windows, the SO_CONDITIONAL_ACCEPT socket option allows the application to have the incoming connections wait until it's accept()ed. The constant (SO_CONDITIONAL_ACCEPT=0x3002) isn't exposed in the Python module, but can be supplied manually:
s.bind(endpoint)
s.setsockopt(socket.SOL_SOCKET, 0x3002, 1)
s.listen(0)
It's so effective that even the first connect is kept waiting.
On macOS, backlog=0 is reset to backlog=SOMAXCONN, backlog=1 keeps all connections except the first waiting.

Trouble authenticating Tor with python

Probably doing something very silly here, but I'm having some trouble authenticating automatically through Tor.
I'm using 32 bit ubuntu 12.04 with obfuscated bridges.
This should be all the relevant code, but let me know if there's something else that would be useful in debugging this issue:
import socket
import socks
import httplib
def connectTor():
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050, True)
#9050 is the Tor proxy port
socket.socket = socks.socksocket
def newIdentity():
socks.setdefaultproxy() #Disconnect from Tor network
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 46594))
s.send("AUTHENTICATE\r\n")
response = s.recv(128)
#128 bytes of data for now, just to see how Tor responds
print response
if response.startswith("250"): #250 is the code for a positive response from Tor
s.send("SIGNAL NEWNYM\r\n") #Use a new identity
s.close()
connectTor() #Just to make sure we're still connected to Tor
Whenever I run this I get the following error:
515 Authentication failed: Password did not match HashedControlPassword value from configuration. Maybe you tried a plain text password
I tried using the --hash-password option and pasting that in the place of the AUTHENTICATE string, but that just caused the script to hang. Thoughts?
That error means that you set the HashedControlPassword option in your torrc. I would suggest option for CookieAuthentication 1 instead then using a controller library rather than doing this from scratch.
What you're trying to do here (issue a NEWNYM) is a very, very common request (1, 2) so I just added a FAQ entry for it. Here's an example using stem...
from stem import Signal
from stem.control import Controller
with Controller.from_port(port = 9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)

Can I register event callbacks using the libvirt Python module with a QEMU backend?

I would like to write some code to monitor events for domains running under QEMU, managed by libvirt. However, trying to register an event handler yields the following error:
>>> import libvirt
>>> conn = libvirt.openReadOnly('qemu:///system')
>>> conn.domainEventRegister(callback, None)
libvir: Remote error : this function is not supported by the connection driver: no event support
("callback" in this case is a stub function that simply prints its arguments.)
The examples I've been able to find regarding libvirt's event handling don't seem to be specific as to which backend hypervisors support which features. Is this expected to work for QEMU backends?
I'm running a Fedora 16 system, which includes libvirt 0.9.6 and qemu-kvm 0.15.1.
For folks finding themselves here via <searchengine>:
UPDATE 2013-10-04
Many months and a few Fedora releases later, the event-test.py code in the libvirt git repository runs correctly on Fedora 19.
Make sure you have registered in the libvirt event loop (or set up your own) before registering for events.
There is a nice example of event handling shipped with the libvirt source (file is called event-test.py). I'm attaching an example based on that code;
import libvirt
import time
import threading
def callback(conn, dom, event, detail, opaque):
print "EVENT: Domain %s(%s) %s %s" % (dom.name(),
dom.ID(),
event,
detail)
eventLoopThread = None
def virEventLoopNativeRun():
while True:
libvirt.virEventRunDefaultImpl()
def virEventLoopNativeStart():
global eventLoopThread
libvirt.virEventRegisterDefaultImpl()
eventLoopThread = threading.Thread(target=virEventLoopNativeRun,
name="libvirtEventLoop")
eventLoopThread.setDaemon(True)
eventLoopThread.start()
if __name__ == '__main__':
virEventLoopNativeStart()
conn = libvirt.openReadOnly('qemu:///system')
conn.domainEventRegister(callback, None)
conn.setKeepAlive(5, 3)
while conn.isAlive() == 1:
time.sleep(1)
Good luck!
//Seto