Hi folks.

I'm currently looking at developing a client app in Python. I have a server app developed in C++ that captures wifi data and then my app connects to the server and grabs the data relating to the wifi access points and then inserts this into a MySQL DB. Here's my code thusfar:

#!/usr/bin/python

import socket
import sys
import re
import MySqldb
import syslog
import logging
import time

# MySQL Server connection settings
conn = MySQLdb.connect (host = "localhost",
                        user = "root",
                        passwd = "toor",
                        db = "testdb")

# Kismet server settings
host = 'localhost'
port = 2501
# Create a TCP socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))  

cursor = conn.cursor ()
cursor.execute ("DROP TABLE IF EXISTS data")
cursor.execute ("""
    CREATE TABLE data
    (
      mac      CHAR(40),
      bssid    CHAR(40),
      contype  CHAR(40),
      ip       CHAR(40),
      ltime    CHAR(40)
     )
   """)

#Start loop to grab client information from Kismet server
while 1:
  s.send('\n!0 enable client mac,bssid,type,ip,lasttime')

  buff = s.recv(512)
  tmp = buff.split()
  mac = tmp[1]
  bssid = tmp[2]
  contype = tmp[3]
  ip = tmp[4]
  ltime = tmp[5]

c.execute(
    """INSERT INTO data (mac, bssid, contype, ip, ltime)
    VALUES (%s, %s, %s, %s, %s)""",
)

conn.close()
cursor.close()
s.close()

My code appears to work (in the sense that it doesn't crash. Likewise, the onscreen printout of the server app shows that the connection is successful. My tables and columns are successfully created too in MySQL. The problem is that the data being sent over the socket (mac, bssid, contype, ip, ltime) are not being inserted into the MySQL DB. The server is detecting wireless access points as the existing client written in C++ confirms this. I have to assume there is an error in my code around this part:-

c.execute(
    """INSERT INTO data (mac, bssid, contype, ip, ltime)
    VALUES (%s, %s, %s, %s, %s)""",

I assume I am missing some key info here which will instruct the app to insert the streamed data into the DB.

Any ideas?

OK so I think the issue must lie with the 'c.execute' command. I've tried editing it to the following but still to no avail:-

c.execute('INSERT INTO data VALUES ("%s","%s","%s","%s","%s")' % (tmp[1],tmp[2],tmp[3],tmp[4],tmp[5]))

Any suggestions?

This is just one of the issues, and you are definitely made a correct move.
Now, the 'c' object in c.execute() is undefined (you have conn, you have cursor, but what is c?). The fact that python doesn't complain means that the c.execute line is never actually executed. It means that most likely the script never progresses beyond s.recv().

This article has been dead for over six months. Start a new discussion instead.