python - Socket.close() doesn't close the Socket -
i'm trying develop simple client/server program, server part working i've problem client part can't understand why.
the client's work simple, retrive counter value external device, i'm trying send retrieved data server part.
at beginning socket working well, time when should send data i've got server exception , after client not working.
i can't understand if s.close() function working properly.
update: exception got "errno 110 connection timed out"
client:
import time, socket, struct, array, json import client_axis import sys import serial import os import datetime import re import packet import config_mio usbport = '/dev/ttyama0' h = "/r/n" if __name__=="__main__": """main function starts server""" curr_value = "0000000000" prev_value = "" line = '111111111111111' error_counter = 0 people_in = 0 people_out = 0 txtdate = "" no_updates_counter = 0 while true: ser = none try: # print('1') ser = serial.serial('/dev/ttyama0', 9600, timeout=1) # ser.open() # print('2') line in ser.read(): line = ser.readline() print(line[6:10]) curr_value = line except: print('serial error') # print('3') pass finally: if ser: ser.close() try: error_counter += 1 # print('1') response = client_axis.read_axis_camera_occupancy() content = response.split(",") people_in_refresh = int(re.search(r'\d+', content[3]).group()) people_out_refresh = int(re.search(r'\d+', content[4]).group()) # print('2') error_flag = 0 if people_in != people_in_refresh or people_out != people_out_refresh: people_in = people_in_refresh people_out = people_out_refresh try: # creates tcp socket in specified ip address , port s = socket.socket(socket.af_inet, socket.sock_stream) # connect client server s.connect((config_mio.ip_server_add, config_mio.ws_port)) # create message , send server timestamp = str(time.time()) # msg = packet("c", timestamp, config_mio.rbpi_id, content[3], content[4], none) msg = "c"+","+str(timestamp)+","+str(config_mio.rbpi_id)+","+str(people_in)+","+str(people_out)+","+"none" # json_message = json.dumps(msg) # s.send(json_message) s.send(msg) print "messaggio inviato" # close connection when data sent #s.close() except: print('server connection error 1') pass finally: s.close() #axis_occup_old = axis_occup #axis_occup = response.read() #my_dict = json.loads(axis_occup) # print(axis_occup) # print(my_dict) #del my_dict["timestamp"] #axis_occup = my_dict #error_counter = 0 # print('3') except: error_msg = "error retrieving occupancy from: " + config_mio.ip_axis_add error_flag = 1 if (error_flag == 1): no_updates_counter = 0 print "error detected: %s \r\n" % error_msg print error_counter if (error_counter > 200): os.system("sudo reboot") elif (line[6:10] != '1111' , prev_value != curr_value): try: # creates tcp socket in specified ip address , port s = socket.socket(socket.af_inet, socket.sock_stream) # connect client server s.connect((config_mio.ip_server_add, config_mio.ws_port)) # create message , send server timestamp = str(time.time()) msg = "r"+","+str(timestamp)+","+str(config_mio.rbpi_id)+","+"none"+","+"none"+","+str(line[6:10]) #msg = {"id": raspberry_id, # "ranging": line[6:10], # "timestamp": timestamp} #json_message = json.dumps(msg) #s.send(json_message) s.send(msg) print "message : %s" % msg # close connection when data sent s.close() except: print('server connection error 2') pass else: no_updates_counter += 1 # send message despite there no changes in value # heartbeat message of 10 min if (no_updates_counter > 200): no_updates_counter = 0 axis_occup = "" prev_value = curr_value # reboot device every day - routine # have 4 cases incase miss few seconds txtdate = str(datetime.datetime.fromtimestamp(time.time())) if (txtdate[11:19] == "00:00:00"): os.system("sudo reboot") if (txtdate[11:19] == "00:00:01"): os.system("sudo reboot") if (txtdate[11:19] == "00:00:02"): os.system("sudo reboot") if (txtdate[11:19] == "00:00:03"): os.system("sudo reboot") # kill time - 1 sec: remove high speed localisation time.sleep(1)
server:
import socket import json import time import config_mio import packet import sqlite3 lite if __name__ == "__main__": """main function starts server""" # creates tcp socket in specified ip address , port s = socket.socket(socket.af_inet, socket.sock_stream) s.bind((config_mio.ip_server_add, config_mio.ws_port)) # starts server, 10 clients queued s.listen(config_mio.max_num_clients) while true: conn, addr = s.accept() #print "sono dopo ascolto" msg = conn.recv(1024) print "data value:",msg msg = msg.split(",") if msg[0] == "c": print "counter msg" elif msg[0] == "r": print "range msg",msg[1],msg[2],msg[5] conn.close()
Comments
Post a Comment