Store data that would be sent over the socket when it disconnects and manage it when socket reconnects - python-2.7

I have a problem where I need to store data on a disk when the TCP socket disconnects for whatever reason that would otherwise be sent over that socket and simultaneously check when a new connection is established and I can't find a solution to this.
What I have is:
def start_server_for_clients(srv, port):
client = None
try:
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.bind(('', port))
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.listen(5)
print('Waiting for clients on port %s' % port)
while True:
client, address = srv.accept()
client.settimeout(10)
print('Connected client %s %s.' % (address[0], address[1]))
t = threading.Thread(target=handle_client, args=(client, address))
t.daemon = True
t.start()
except Exception as e:
print(e)
if client:
client.close()
handle_client is a function where data is processed and sent over the socket every second in a while loop. I need that processed data either way: connected or not and store it.
Now my logic to separate data processing and socket communication into different parts but where does it fit into threading once the connection is back up again because I would need to clear off the stored data first while simultaneously storing/buffering newly accumulated data and only then proceed to instant communication between sockets? Do I need to use select in this thread and spawn another one to for storing and recovering?

Related

Python 2.7 ignoring if not statement?

I'm creating a simple socket chatting application and I tried to make something that handles somebody terminating the client connected to the server, but it seems to ignore it and I get the connection forcibly closed by the remote host exception.
Here's some of the code
clients = []
class client(Thread):
def __init__(self, socket, address):
Thread.__init__(self)
self.sock = socket
self.addr = address
self.start()
def run(self):
while 1:
msg = self.sock.recv(1024).decode()
#This if not statement is ignored
if not msg:
clients.remove(socket)
print str(adress[0]) + ":" + str(adress[1]) + " disconnected"
quitm = str(adress[0]) + ":" + str(adress[1]) + " disconnected"
for client in clients:
client.send(quitm.encode())
socket.close()
break
print msg
for client in clients:
client.send(msg.encode())
I've been searching up for some solutions but haven't found any. Any help is appreciated!!!
If the thing at the other end closes the connection normally, that is indicated by recv() returning zero bytes. However, if the thing at the other end terminates without closing the connection, that is considered an abnormal situation which indicated by recv() raising an exception. You need to use a try/catch block to handle that exception.

How to send a captured packet saved as text with Python

I captured packets on individual text files with tcpdump, I want to send back the captured packets, first I extracted the IP and Port, but I have not been able to send the packet.
This is my code:
def client():
packet = open("packet3.txt", "r")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
sock.connect(("192.168.128.1", 80))
while True:
try:
sock.send("packet")
sleep(1)
reply = sock.recv(131072)
if not reply:
break
print "recvd: ", reply
except KeyboardInterrupt:
print "bye"
break
sock.close()
return
client()
I get this error:
reply = sock.recv(131072)
error: [Errno 10054] An existing connection was forcibly closed by the remote host

How to send events from siddhi event simulator to android siddhi app

I have a siddhi cep application running on Android. Now I want to send events from event simulator from stream processing editor to android app via a socket connection. Till now I have been successful in making android server socket which listens to python client simulator made by me. But to ease the process, is it possible that I can use event simulator to send events to android siddhi app?
I was wondering if I can change some configurations such that event simulator sends events to android socket, so I looked at setting in deployment.yaml file
but the sending configurations are defined for HTTP
senderConfigurations:
-
id: "http-sender"
# Configuration used for the databridge communication
databridge.config:
# No of worker threads to consume events
# THIS IS A MANDATORY FIELD
workerThreads: 10
# Maximum amount of messages that can be queued internally in MB
# THIS IS A MANDATORY FIELD
maxEventBufferCapacity: 10000000
# Queue size; the maximum number of events that can be stored in the queue
# THIS IS A MANDATORY FIELD
eventBufferSize: 2000
# Keystore file path
# THIS IS A MANDATORY FIELD
keyStoreLocation : ${sys:carbon.home}/resources/security/wso2carbon.jks
# Keystore password
# THIS IS A MANDATORY FIELD
keyStorePassword : wso2carbon
# Session Timeout value in mins
# THIS IS A MANDATORY FIELD
clientTimeoutMin: 30
# Data receiver configurations
# THIS IS A MANDATO
Thanks in advance. If you need some more details please let me know
Edit 1
I actually found a way around to do it but it's having some issues. So basically I redirected output sink of event generator to port such that sink has all the data streams. The code for Stream Processor Studio editor is
#App:name("PatternMatching")
#App:description('Identify event patterns based on the order of event arrival')
define stream RoomTemperatureStream(roomNo string, temp double);
#sink(type="tcp", url='tcp://localhost:5001/abc', sync='false', tcp.no.delay='true', keep.alive='true', worker.threads="10", #map(type='text'))
define stream RoomTemperatureAlertStream(roomNo string, initialTemp double, finalTemp double);
--Capture a pattern where the temperature of a room increases by 5 degrees within 2 minutes
#info(name='query1')
from RoomTemperatureStream
select e1.roomNo, e1.temp as initialTemp, e2.temp as finalTemp
insert into RoomTemperatureAlertStream;
it sends the streams as text to python server, which needs to be started first, code of which is
#!/usr/bin/env python
# Author : Amarjit Singh
import pickle
import socket
import pandas
from pandas import json
if __name__ == "__main__":
# ------------------ create a socket object-----------------------#
try:
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as err:
serversocket.close()
print("socket creation failed with error %s" % (err))
except KeyboardInterrupt:
serversocket.close()
print("KeyboardInterrupt - but server socket was closed ")
host = "127.0.0.1"
Server_port = 5001
# ------------------ binding to the port -----------------------#
try:
serversocket.bind((host, Server_port))
serversocket.listen(50) # queue up to 5 requests
print("\n Server has started and waiting for connection request ..... ")
# bind to the port
while True: # extra while is created so that server runs even if there is no data
running = True
clientsocket, addr = serversocket.accept() # accept a connection from client
print("Got a connection from Server%s" % str(addr)) # show connection success message
while running:
receivedData = clientsocket.recv(2048)
# json = receivedData
if receivedData:
print(receivedData)
print(receivedData[0])
print(receivedData[1])
print(receivedData[2])
# roomNo = str(receivedData[0])
# temp = int(client_tuple[1]) # from unicode to int
#
# print(" roomNo = %d: UUID = %s temp = %d" % (roomNo, temp))
except socket.error as err:
serversocket.close()
print("socket creation failed with error %s" % (err))
except KeyboardInterrupt:
serversocket.close()
print("KeyboardInterrupt - but server socket was closed ")
Initially, I was sending json data from simulator, but pickle.loads and json.loads did not work. but the problem with text is that data is displayed as
b'\x02\x00\x00\x00t\x00\x00\x003bed14d31-6697-4a74-8a3f-30dc012914ad-localhost:5001\x00\x00\x00\x03abc\x00\x00\x002roomNo:"X0ZYp",\ninitialTemp:15.97,\nfinalTemp:17.22'
b'\x02\x00\x00\x00t\x00\x00\x003bed14d31-6697-4a74-8a3f-30dc012914ad-localhost:5001\x00\x00\x00\x03abc\x00\x00\x002roomNo:"2X951",\ninitialTemp:13.42,\nfinalTemp:10.76'
b'\x02\x00\x00\x00t\x00\x00\x003bed14d31-6697-4a74-8a3f-30dc012914ad-localhost:5001\x00\x00\x00\x03abc\x00\x00\x002roomNo:"PUaJA",\ninitialTemp:15.46,\nfinalTemp:16.26'
b'\x02\x00\x00\x00t\x00\x00\x003bed14d31-6697-4a74-8a3f-30dc012914ad-localhost:5001\x00\x00\x00\x03abc\x00\x00\x002roomNo:"pnz0i",\ninitialTemp:10.42,\nfinalTemp:15.82'
how to remove this extra data?
Siddhi has a WebSocket connector[1] and it's still WIP. Using this dependency you will be able to add a WebSocket sink to your app and send events from that.
Unfortunately, you cannot directly send events from Stream Processor[2] Studio/Editor but if you have an app running in Stream Processor Editor and if it has a WebSocket sink then you can send events to App's sink stream from the Simulator which will intern send that message via WebSocket to the Siddhi app in android.
You can only simulate apps running inside the editor via the Event Simulator or simulate apps deployed in Stream Processor worker nodes via Event Simulator API.
[1]https://github.com/wso2-extensions/siddhi-io-websocket
[2]https://wso2.com/analytics

why python does not send anything with sockets? (basic sockets)

As you can see in images below i cannot reciveve anything from client and server keeps waiting! I have tried it without firewall and no result.. :(
cmd info
Client
import socket
sock1 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock1.sendto("HOLA",('192.168.0.159',25585))
sock1.close()
del sock1
Server
import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind(('0.0.0.0',25585))
while True:
data , c = sock.recvfrom(1024);
print data
sock.close()
del sock
Your client and your server are not connected. Try this:
client:
import socket
sock1 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock1.connect(('192.168.0.159',25585))
sock1.sendto("HOLA",('192.168.0.159',25585))
sock1.close()
del sock1
Your server code is good
Otherwise UDP sockets are mostly known for packets dropping

server socket error timer management

There is a server for a single client. Client can connect and disconnect at any time
Here is simplified code
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
while true:
a = s.accept()
try:
data = a[0].recv(1024)
except socket.error:
a[0].close()
print 'cought the error'
It works, but socket.error generation takes random amount of time from few seconds up to a minute. May this value be managed?
If the client disconnects, there is no socket.error, rather recv() returns an empty string immediately. Besides, you are not closing the connection in this case.