Errors with LCD screen - python

I've got a problem with a LCD screen HD44780. The screen is connected to RPi3 via I2C. I've prepared a code which shows data on it.
from thread import *
import socket
import sys
import string
import select
import lcddriver
import MySQLdb
import time
import mysql.connector
BUFFER_SIZE = 1024
display = lcddriver.lcd()
database = 'data'
username = 'admin'
password = 'mypasss'
date_now = time.strftime("%Y-%m-%d %H:%M:%S")
def clientthread(conn):
received = []
while True:
data = conn.recv(BUFFER_SIZE)
if data == ";" :
conn.close()
if received[0] == '1':
esp_id = int(received[0])
temperature = float (received[1] + received[2] + received[3] + received[4])
humidity = float (received[5] + received[6] + received[7] + received[8])
esp1 = received[0] + ":T=" + received[1] + received[2] + received[3] + received[4] + "H=" + received[5] + received[6] + received[7] + received[8]
display.lcd_display_string(str(esp1), 1)
print esp1
datehour = time.strftime("%Y-%m-%d %H:%M:%S")
db = MySQLdb.connect("localhost","root","raspberry","projekt_grupowy")
cursor = db.cursor()
sql = "INSERT INTO pomiary(esp_id, temperature, humidity, datehour) VALUES('%d', '%.2f', '%.2f', '%s')" % (esp_id, temperature, humidity, datehour)
try:
cursor.execute(sql)
db.commit()
except:
print "Nieudana proba wpisu"
db.rollback()
else:
print "Data sent"
received = []
break
elif received[0] == '2':
esp_id = int (received[0])
temperature = float(received[1] + received[2] + received[3] + received[4])
humidity = float(received[5] + received[6] + received[7] + received[8])
esp2 = received[0] + ":T=" + received[1] + received[2] + received[3] + received[4] + "H=" + received[5] + received[6] + received[7] + received[8]
print esp2
display.lcd_display_string(str(esp2), 2)
datehour = time.strftime("%Y-%m-%d %H:%M:%S")
db = MySQLdb.connect("localhost","root","raspberry","projekt_grupowy")
cursor = db.cursor()
sql = "INSERT INTO pomiary(esp_id, temperature, humidity, datehour) VALUES('%d', '%.2f', '%.2f', '%s')" % (esp_id, temperature, humidity, datehour)
try:
cursor.execute(sql)
db.commit()
except:
print "Nieudana proba wpisu"
db.rollback()
else:
print "Data sent"
received = []
break
else:
print "error sensor"
received = []
else:
received.append(data)
conn.close()
def main():
try:
host = '192.168.42.1'
port = 8888
tot_socket = 1
list_sock = []
for i in range(tot_socket):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((host, port+i))
s.listen(10)
list_sock.append(s)
print "[*] Server listening on %s %d" %(host, (port+i))
while 1:
for j in range(len(list_sock)):
conn, addr = list_sock[j].accept()
print '[*] Connected with ' + addr[0] + ':' + str(addr[1])
start_new_thread(clientthread ,(conn,))
s.close()
except KeyboardInterrupt as msg:
sys.exit(0)
if __name__ == "__main__":
main()
I receive data from two ESP modules and show them on the screen. 3
display.lcd_display_string(str(esp1), 1)
display.lcd_display_string(str(esp2), 2)
Sometimes there's a moment when two modules are sending data at the same time. In result the screen shows errors even though I've implemented multithreading. What can I do to avoid that?

Related

Django Exception Hook

is possible to handle all traceback/exception error code in django ?
i try using sys.excepthook = myfunction in the first line manage.py, which my function/method/class provides python functions/methods like send log to syslog and other functions, here's an example of my exception hook function:
#!/usr/bin/env python
# ehook.py
from __future__ import print_function
from make_colors import make_colors
from xnotify import notify
import os, sys
import socket
from configset import configset
import syslogx
import datetime
import re
import traceback
from pydebugger.debug import debug
PID = os.getpid()
class ehook:
CONFIGNAME = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ehook.ini')
CONFIG = configset(CONFIGNAME)
def __init__(self, exc_type, exc_value, tb, **kwargs):
#local_vars = {}
#super(ehook, self)
self.exc_type = exc_type
self.exc_value = exc_value
self.tb = tb
data, data_color, raw_data, raw_data_color = self.tracert()
app = "ehook"
title = "Traceback"
event = 'Error'
icon = list(filter(lambda k: os.path.isfile(k), [os.path.splitext(os.path.realpath(__file__))[0] + "." + i for i in ['png', 'jpg']] + [os.path.join(os.path.dirname(os.path.realpath(__file__)), 'traceback') + "." + i for i in ['png', 'jpg']]))
#print("icon:", icon)
kwargs.update({'app': app,})
kwargs.update({'title': title,})
kwargs.update({'event': event,})
kwargs.update({'sticky': True,})
if self.CONFIG.get_config('syslog', 'severity'):
kwargs.update({'syslog_severity': self.CONFIG.get_config('syslog', 'severity').lower(),})
if self.CONFIG.get_config('syslog', 'facility'):
kwargs.update({'syslog_facility': self.CONFIG.get_config('syslog', 'facility').lower(),})
if icon: kwargs.update({'icon': icon[0],})
#icon = os.path.join(os.path.dirname(os.path.realpath(__file__)), icon)
syslog_server = re.split("\n|\t|,", self.CONFIG.get_config('syslog', 'host')) or kwargs.get('syslog_server')
syslog_server = [i.strip() for i in syslog_server]
debug(syslog_server = syslog_server)
if isinstance(syslog_server, list):
for i in syslog_server:
self.debug_server_client(data, data_color, i.split(":")[0])
else:
self.debug_server_client(data, data_color, syslog_server)
kwargs.update({'syslog_server': syslog_server,})
notify.send(message_color = data_color, message = "\n".join(raw_data[1:]), **kwargs)
def sent_to_syslog(self, message, severity=None, facility=None, host = None, port = None):
severity = severity or 3
facility = facility or 3
host = host or self.CONFIG.get_config('syslog', 'host') or '127.0.0.1'
port = port or self.CONFIG.get_config('syslog', 'port') or 514
if "," in host:
host = [i.strip() for i in host.split(",")]
else:
if not isinstance(host, list): host = [host]
if hasattr(message, 'decode'):
message = message.decode('utf-8')
for i in host:
if ":" in i:
HOST, PORT = str(i).split(":")
HOST = HOST.strip()
PORT = PORT.strip()
PORT = PORT or 514
print("sent to %s:%s" % (HOST, str(PORT)))
syslogx.syslog(message, severity, facility, HOST, int(PORT))
else:
syslogx.syslog(message, severity, facility, i, port)
def debug_server_client(self, msg, msg_color = None, host = None, port = None):
dtime = make_colors(datetime.datetime.strftime(datetime.datetime.now(), '%Y:%m:%d'), 'white', 'red') + \
make_colors('~', 'white') + \
make_colors(datetime.datetime.strftime(datetime.datetime.now(), '%H:%M:%S'), 'white', 'green') + \
make_colors(datetime.datetime.strftime(datetime.datetime.now(), '%f'), 'white', 'magenta') + \
"[%s]" % PID
if sys.version_info.major == 3:
msg = msg.encode('utf-8')
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = host or self.CONFIG.get_config('server', 'host') or '127.0.0.1'
port = port or self.CONFIG.get_config('server', 'port') or 50000
if not host:
try:
if sys.version_info.major == 3:
msg_color = bytes(dtime + " " + msg_color, encoding = 'utf-8')
else:
msg_color = dtime + " " + msg_color
s.sendto((msg_color or msg), (host, port))
except:
traceback.format_exc()
print(msg)
self.sent_to_syslog(msg)
s.close()
#return False
if "," in host:
host = [i.strip() for i in host.split(",")]
else:
host = [host]
for i in host:
if ":" in i:
host1, port1 = str(i).strip().split(":")
port1 = int(port1.strip()) or port
host1 = host1.strip()
try:
if sys.version_info.major == 3:
msg_color = bytes(dtime + " " + msg_color, encoding = 'utf-8')
else:
msg_color = dtime + " " + msg_color
s.sendto((msg_color or msg), (host1, port1))
except:
traceback.format_exc()
print(msg)
self.sent_to_syslog(msg)
s.close()
#print "%s:%s 0" % (host, str(port))
break
else:
host = i.strip()
#print "%s:%s 1" % (host, str(port))
if sys.version_info.major == 3:
msg_color = bytes(dtime + " " + msg_color, encoding = 'utf-8')
else:
msg_color = dtime + " " + msg_color
s.sendto((msg_color or msg), (host, port))
s.close()
break
def tracert(self):
trace = ["Traceback: "]
trace_color = []
while self.tb:
filename = self.tb.tb_frame.f_code.co_filename
name = self.tb.tb_frame.f_code.co_name
line_no = self.tb.tb_lineno
data_color = make_colors("Traceback:", 'b', 'y') + " " + \
make_colors(self.exc_type.__name__, 'lw', 'r') + " (" + \
make_colors(self.exc_value, 'lw', 'm') + "), File " + \
make_colors(filename, 'b', 'lg') + ", line " + \
make_colors(line_no, 'lw', 'bl') + ", in " + \
make_colors(name, 'b', 'lc')
print(data_color)
trace_color.append(data_color)
data = str(self.exc_type.__name__) + ": " + str(self.exc_value) + "), File " + filename + ", line " + str(line_no) + ", in " + name
trace.append(data)
#print(f"File {filename} line {line_no}, in {name}")
#local_vars = tb.tb_frame.f_locals
#print(f"{exc_type.__name__}, Message: {exc_value}")
self.tb = self.tb.tb_next
#print(f"Local variables in top frame: {local_vars}")
return "\n".join(trace), "\n".join(trace_color), trace, trace_color
in manage.py
from ehook import ehook
sys.excepthook = ehook
import sys
import os
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tobi.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
but it doesn't work like what I expected
how to handle all exceptions in django ?

Blocking sockets in python

I'm trying to write a program. I writting gps tracking program in python. I want to listen to a continuous port. I wrote something for that. But it doesn't constantly log out or save data to the database. Where's the problem?
import socket
import MySQLdb
import sys
import thread
from datetime import datetime, timedelta
con = None
dbhost = 'localhost'
dbport = 3306
dbuser = "root"
dbpass = ""
dbname = "test_db"
def on_new_client(clientsocket,addr):
cik = 0
while cik != 1:
paketler = [20]
msg = clientsocket.recv(1024)
dosya = open('/etc/asdf/taxi.txt','a')
dosya.write('\n')
dosya.write(msg)
dosya.flush()
dosya.close()
message1 = msg.split("#")
i=0
j=len(message1)
while i < (j-1):
try:
liste= message1[i].split(";")
location= liste[0].split(",")
if(location[0]=='$DATA' and str(location[2])!='' and location[3]!='00002000' and len(liste)==14):
way= liste[13].split(",")
date_data= str(location[3])+" "+str(liste[1])
data = datetime.strptime(date_data,'%d%m%Y %H%M%S')
data = data + timedelta(hours=3)
datetim = data.strftime('%d-%m-%Y %H:%M:%S')
datetim = data.strftime('%Y-%m-%d %H:%M:%S')
stat = liste[12].split(",")
if (len(stat)==2 and stat[1]!=''):
#insert in db
elif(location[0]=='$GEOLOC' and str(location[2])!='' and len(liste)==14):
way= liste[13].split(",")
date_data= str(liste[1])+" "+str(liste[2])
data = datetime.strptime(date_data,'%d%m%Y %H%M%S')
data = data + timedelta(hours=3)
datetim = data.strftime('%d-%m-%Y %H:%M:%S')
datetim = data.strftime('%Y-%m-%d %H:%M:%S')
elif(location[0]=='$TRIP' and str(location[2])!='' and len(liste)==32):
date_data= str(location[3])+" "+str(liste[1])
data = datetime.strptime(date_data,'%d%m%Y %H%M%S')
data = data + timedelta(hours=3)
datetim = data.strftime('%d-%m-%Y %H:%M:%S')
datetim = data.strftime('%Y-%m-%d %H:%M:%S')
date_data= str(liste[11])+" "+str(liste[12])
data = datetime.strptime(date_data,'%d%m%Y %H%M%S')
data = data + timedelta(hours=3)
datetim2 = data.strftime('%d-%m-%Y %H:%M:%S')
datetim2 = data.strftime('%Y-%m-%d %H:%M:%S')
#insert in db
i = i+1
except:
print "Exit"
clientsocket.close()
cik = 1
break
print addr, ' >> ', msg
clientsocket.close()
s = socket.socket() # Create a socket object
host = '127.0.0.1' # Get local machine name
port = 1234 # Reserve a port for your service.
con = MySQLdb.connect(dbhost, dbuser, dbpass, dbname);
cur = con.cursor()
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client
thread.start_new_thread(on_new_client,(c,addr))
con.close()
s.close()
if msg > clientsocket.recv(1024) program is stop. I don't want the program to stop.

Firebird statements no error but no result

Running Firebird on my Raspberry Pi at the moment and using FlameRobin to control it. With another Raspberry Pi I want to some statements to it, I don't get any error codes but the data just doesn't get there.
import RPi.GPIO as GPIO
import time
import fdb
con = fdb.connect(dsn='10.100.2.197/3050:/home/trainee2/Desktop/ice', user='sysdba', password='trainee')
text_file = open("namen1.txt", "r")
lines = text_file.read().split(',')
namen = lines
text_file.close()
status = [0] * 12
indexSpatie = 0
pinnen = [18,23,24,25,20,21,17,27,6,13,19,26]
controlepin = [1] * 12
GPIO.setmode(GPIO.BCM)
for p in range(0,12):
GPIO.setup(pinnen[p],GPIO.IN)
print pinnen[p]
cur = con.cursor()
while True:
for e in range(0,12):
status[e] = GPIO.input(pinnen[e])
if (status[e] != controlepin[e]):
n = e
naam = str(namen[n])
indexSpatie = naam.index(' ')
voornaam = naam[:indexSpatie]
achternaam = naam[indexSpatie:]
stat = str(status[n])
datum = time.strftime("%d/%m/%Y")
print( voornaam + achternaam + " met pinnummer: " + str(pinnen[n]) + " heeft status van " + stat + " op vandaag: " + datum)
cur.execute("insert into ICEDATA (PRENAME, NAME, DATUM) values(?,?,?)",(voornaam,achternaam,datum))
controlepin[e] = status[e]
time.sleep(1)
I post the whole code cause I don't see anything wrong with the statement and connection itself.
Like #ain said, forgot the commit statement on the connection
con.commit()

How to end a server socket listen

So what i'm trying to do is to make a multi-threaded server that creates threads for each client that connects to it, and replies back the string sent from the client.
It sort of works, but my server doesn't actually end properly. My KerboardInterrupt catch doesn't seem to work in windows command prompt, only thing that lets me exit the process would be ctrl + pause/break. Can anyone help me think of a way to get the server to end gracefully?
Server Code:
import socket
import threading
import time
import datetime
import sys
def getTime():
ts = time.time()
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m- %d_%H:%M:%S')
return timeStamp
def ThreadFunction(clientsocket, clientaddr):
global ReceivedData
global SentData
while True:
#Receive data from client
data = clientsocket.recv(bufferSize)
#Get client IP and port
clientIP, clientSocket = clientsocket.getpeername()
#Add to total amount of data transfered
ReceiveDataSize = len(data)
ReceivedData += ReceiveDataSize
#LOg the received data
text_file.write(str(getTime()) + "__ Size of data received (" + clientIP + ":" + str(clientSocket) + ") = " + str(ReceiveDataSize) + '\n')
#Send data
clientsocket.send(data)
SentDataSize = len(data)
SentData += SentDataSize
#Log the sent data
text_file.write(str(getTime()) + "__ Size of data sent (" + clientIP + ":" + str(clientSocket) + ") = " + str(SentDataSize) + '\n')
def Close(counter, ReceivedData, SentData):
print ("Shutting down Server...")
serversocket.close()
text_file.write("\n\nTotal # of connections: " + str(counter))
text_file.write("\nTotal data received: " + str(ReceivedData))
text_file.write("\nTotal data sent: " + str(SentData))
text_file.close()
sys.exit()
if __name__ == '__main__':
serverIP = raw_input('Enter your server IP \n')
port = int(raw_input('What port would you like to use?\n'))
# Maintain how many connections
connections = []
counter = 0
# Maintain amount of data sent to and from server
ReceivedData = 0
SentData = 0
bufferSize = 1024
# Create and initialize the text file with the date in the filename in the logfiles directory
text_file = open("MultiThreadedServerLog.txt", "w")
address = (serverIP, port)
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind server to port
serversocket.bind(address)
# The listen backlog queue size
serversocket.listen(50)
print ("Server is listening for connections\n")
try:
while 1:
# Accept client connections, increment number of connections
clientsocket, clientaddr = serversocket.accept()
counter += 1
# Log client information
print (str(clientaddr) + " : " + " Just Connected. \n Currently connected clients: " + str(counter) + "\n")
text_file.write(str(getTime()) + " - " + str(clientaddr) + " : " + " Just Connected. \n Currently connected clients: " + str(counter) + "\n")
clientThread = threading.Thread(target=ThreadFunction, args=(clientsocket, clientaddr))
clientThread.start()
except KeyboardInterrupt:
print ("Keyboard interrupt occurred.")
Close(counter, ReceivedData, SentData)
Client Code:
from socket import *
import threading
import time
import random
import sys
import datetime
serverIP = ""
port = 8005
message = ""
msgMultiple = 1
def getTime():
ts = time.time()
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H:%M:%S')
return timeStamp
def run(clientNumber):
buffer = 1024
global totalTime
s = socket(AF_INET, SOCK_STREAM)
s.connect((serverIP, port))
threadRTT = 0
while 1:
for _ in range(msgMultiple):
cData = message + " From: Client " + str(clientNumber)
# Start timer and send data
start = time.time()
s.send(cData.encode('utf-8'))
print "Sent: " + cData
# Stop timer when data is received
sData = s.recv(buffer)
end = time.time()
# Keep track of RTT and update total time
response_time = end - start
threadRTT += end - start
totalTime += response_time
print "Received: " + cData + '\n'
t = random.randint(0, 9)
time.sleep(t)
# Log information of Client
text_file.write(
"\nClient " + str(clientNumber) + " RTT time taken for " + str(msgMultiple) + " messages was: " + str(
threadRTT) + " seconds.")
threadRTT = 0
break
if __name__ == '__main__':
serverIP = raw_input('Enter the server IP: ')
port = int(input('Enter the port: '))
clients = int(input('Enter number of clients: '))
message = raw_input('Enter a message to send: ')
msgMultiple = int(input('Enter the number of times you would like to send the message: '))
# Initialize Log file
text_file = open("ClientLog.txt", "w")
# Used to maintain list of all running threads
threads = []
totalTime = 0
# Create a seperate thread for each client
for x in range(clients):
thread = threading.Thread(target=run, args=[x])
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
# Calculations for log data
bytes = sys.getsizeof(message)
totalRequests = clients * msgMultiple
totalBytes = totalRequests * bytes
averageRTT = totalTime / totalRequests
# Output data
print("Bytes sent in message was : " + str(bytes))
print("Total Data sent was : " + str(totalBytes) + " Bytes.")
print("Average RTT was : " + str(averageRTT) + " seconds.")
print("Requests was : " + str(totalRequests))
# Write data to log file
text_file.write("\n\n Bytes sent in message was : " + str(bytes))
text_file.write("\nTotal Data sent was : " + str(totalBytes) + " Bytes.")
text_file.write("\nAverage RTT was : " + str(averageRTT) + " seconds.")
text_file.write("\nRequests was : " + str(totalRequests))
Also if anyone else has any general improvements they would add to this code, let me know. I'm still pretty new at python, and still rough at it.
Here is the normal intended input i'm getting from my server.
But when it gets to the last client that connects, it starts to drag on for some reason.
The last picture, the inputs go on for the majority of the text file, for a very long time. Seems like something isn't ending properly.
Solved by adding an if statement that checks for a byte < 0 , if it is, end the socket.

Python Multi-threaded socket listener error with threads not releasing

I have 500+ units in the world that connects to my server and dump their data. Up to now i have been using a PHP script to act as a socket listener, but I need to go multi-threaded as the load is increasing and PHP can not keep up. I am quite new to Python and decided to use it for my new platform, over the past few days i have struggled and tried many examples to no avail. Through my search i came across some questions trying to answer this problem, but none did. I will attach my code.
The problem : as the units connect to the server and the server accepts it, the server creates a new thread to handle the connection, the problem comes when the unit drops the connection the thread stays open and active and the total thread count grows, and this is linked to the system limit : "number of open files", i can increase this limit but this only make it a time bomb , it does not solve this.
Please help.
#! /usr/bin/python
import multiprocessing
import socket
import sys
import pprint
import datetime
import MySQLdb
import time
import datetime
import re
import select
import resource
import threading
max_connections = 1024
max_connections_set = max_connections
resource.setrlimit(resource.RLIMIT_NOFILE, (max_connections_set, max_connections_set))
#the incomming port
the_global_port = xxxx #(any port)
#display id
the_global_id = "UNIT TYPE"
class ConnectionObject(object):
the_id = None
the_socket = None
the_socket_address = None
the_socket_address_ip = None
the_socket_address_port = None
the_server = None
the_process = None
the_process_id = None
the_process_name = None
the_imei = None
identifier = ""
# The class "constructor" - It's actually an initializer
def __init__(self, in_process_nr, in_process , in_socket , in_socket_address, in_server):
self.the_id = in_process_nr
self.the_process = in_process
self.the_process_id = self.the_process.exitcode
self.the_process_name = self.the_process.name
self.the_socket = in_socket
self.the_socket_address = in_socket_address
self.the_socket_address_ip = self.the_socket_address[0]
self.the_socket_address_port = self.the_socket_address[1]
self.the_server = in_server
self.identifier = str(self.the_id) + " " + str(self.the_process_name) + " " + str(self.the_socket_address_ip) + " " + str(self.the_socket_address_port) + " "
#end def init
#end def class
def processData(the_connection_object , the_data):
def mysql_open_connection_inside():
try:
the_conn = MySQLdb.connect(host= "127.0.0.1",
user="user",
passwd="password",
db="mydb")
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
time.sleep(30)
try:
the_conn = MySQLdb.connect(host= "127.0.0.1",
user="user",
passwd="password",
db="mydb")
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
print "Unexpected error:", sys.exc_info()[0]
raise
sys.exit(0)
#end time 2
#end try except
return the_conn
#end def mysql_open_connection
conn = mysql_open_connection_inside()
x = conn.cursor()
add_rawdata = ("INSERT INTO RawData"
"(ID,RawData,Type) "
"VALUES (%s, %s, %s)")
data_raw = ('123456', 'ABCD','')
records_inserted = 0
the_connection_object.the_imei = ""
#global clients
imei = ""
try:
thedata = ""
thedata = " ".join("{:02x}".format(ord(c)) for c in the_data)
record_to_save = ' '.join(thedata)
seqtoreply = ""
seqtoreply = "OK"
#reply part
if (seqtoreply != ""): #reply to unit
try:
the_connection_object.the_socket.sendall(seqtoreply)
#echoout(the_connection_object.identifier+"We Replyed With : " + seqtoreply)
except:
echoout(the_connection_object.identifier+"Send Reply Error : " + str(sys.exc_info()[1]))
#end except
#end of if
the_id = "some generated id"
data_raw = (the_id, werk_data, 'UNIT')
try:
x.execute(add_rawdata, data_raw)
conn.commit()
echoout(the_connection_object.identifier+"Raw Data Saved.")
except:
conn.rollback()
echoout(the_connection_object.identifier+" Raw Data NOT Saved : " + str(sys.exc_info()[1]))
#end of data save insert
#echoout("=============================")
endme = 1
echoout("")
conn.close()
#end try
except:
conn.close()
echoout(the_connection_object.identifier+"Error : " + str(sys.exc_info()[1]))
#end try except
#end def handel function
def handle_p(processnr, server, connection, address):
this_connection = ConnectionObject(processnr,multiprocessing.current_process(), connection, address, server)
thisprocess = multiprocessing.current_process()
this_connection.the_id = ""
the_address = this_connection.the_socket_address_ip
the_port = this_connection.the_socket_address_port
try:
echoout("New connection from : "+str(the_address)+" on port "+str(the_port))
close_the_socket = False
while True:
#--------------------- recive part -------------------------------------------------
data = connection.recv(512)
thedata = ""
thedata = " ".join("{:02x}".format(ord(c)) for c in data)
if ((thedata == "") or (thedata == " ") or (data == False)):
echoout("Socket Closed Remotely : No Data")
close_the_socket = True
break
#end - if data blank
else :
processData(this_connection, data)
#end there is data
echoout("=============================")
#end if while true
#end try
except:
print "handling request, Error : " + str(sys.exc_info()[1])
close_the_socket = True
connection.close()
finally:
close_the_socket = True
echoout("Closing socket")
connection.close()
#end try finally
#end def handel function
def mysql_update(update_statement, update_values):
conn_update = MySQLdb.connect(host= "127.0.0.1",
user="user",
passwd="password",
db="mydb")
x_update = conn_update.cursor(MySQLdb.cursors.DictCursor)
rawdata_data = (update_statement)
data_rawdata = (update_values)
allupdateok = False
#end if there is more
try:
x_update.execute(rawdata_data, data_rawdata)
conn_update.commit()
allupdateok = True
conn_update.close()
except:
conn_update.rollback()
allupdateok = False
conn_update.close()
print "Unexpected error:", sys.exc_info()[0]
raise
#end of data save insert
if (allupdateok == False):
echoout("Update Raw Data Table Error")
#end if update
return allupdateok
#end def mysqlupdate
def echoout(thestring):
datestring = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if (thestring != ""):
outstring = datestring + " " + thestring
print outstring
else :
outstring = thestring
print outstring
#end - def echoout
class Server(object):
threads = []
all_threads = []
high_proc = ""
def __init__(self, hostname, port):
self.hostname = hostname
self.port = port
def start(self):
echoout("Listening for conncetions")
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((self.hostname, self.port))
self.socket.listen(10)
process_number = 1
inputs = [self.socket]
while True:
inready, outready, excready = select.select(inputs, [], [], 30);
for s in inready:
if s == self.socket:
conn, address = self.socket.accept()
high_processname = ""
echoout("Got a connection...")
process = threading.Thread(target=handle_p, args=(process_number,self, conn, address))
high_processname = process.name
self.high_proc = high_processname
process.daemon = True
process.start()
self.all_threads.append((process,conn))
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
self.threads.append((process_number,conn,st,0,process))
process_number = process_number + 1
print "ACTIVE Threads = " + str(threading.activeCount())
the_total_count = 0
dead_count = 0
alive_count = 0
for the_thread in self.all_threads :
if (the_thread[0].is_alive() == True):
alive_count = alive_count + 1
else :
dead_count = dead_count + 1
the_thread[1].close()
the_thread[0].join(0.3)
self.all_threads.pop(the_total_count)
#end if alive else
the_total_count = the_total_count + 1
#end for threads
print "LIVE Threads = " + str(alive_count)
print "DEAD Threads = " + str(dead_count)
print "TOTAL Threads = " + str(the_total_count)
print ""
#end if s = socke, new connection
#end for loop
#end while truw
self.socket.close()
#end def start
#main process part
if __name__ == "__main__":
start_ip = "0.0.0.0"
start_port = the_global_port
#start server
server = Server(start_ip, start_port)
try:
print "Listening on " , start_port
server.start()
except:
print "unexpected, Error : " + str(sys.exc_info()[1])
finally:
print "shutting down"
active_clients = 0
for process in multiprocessing.active_children():
try:
active_clients = active_clients + 1
process.terminate()
#process.join()
except:
print "Process not killed = " + str(sys.exc_info()[1])
#end try except
#close mysql connection
print "Active clients = " + str(active_clients)
#end try finally
server.socket.close()
server.threads = []
server = None
print "All done."
#end def main
First of all, it is silly to use threads when you can have 500+ connected clients, you should go asynchronous - look at gevent for example for
a very good library, or at least use select (see Python documentation).
Then, your code to close the socket in handle_p looks good, indeed when
the recv() call comes back with an empty string it means the remote
end is disconnected so you break the while, fine.
However, it looks like the remote closed the connection but it is not
detected on your side (recv() doesn't return). The best would be
then to have a kind of heartbeat to know when you can close the
connection.

Categories