python中的阻塞套接字

2024-06-01 05:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个程序。我正在用python编写gps跟踪程序。我想收听连续端口。我为此写了一些东西。但它不会经常注销或将数据保存到数据库中。问题出在哪里

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()

如果msg > clientsocket.recv(1024)程序停止。我不想让程序停止


Tags: andimportdatadatetimedatemsglocationtimedelta