1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Python will not connect to mysql database

Discussion in 'Tomato Firmware' started by Berto, Jul 6, 2014.

  1. Berto

    Berto Network Newbie Member

    Hi,

    I have a python script that will make a connection to a external MySQL database.
    In Python is that possible if you do a 'import MySQLdb' or 'import mysql.connector'

    I found the source of mysql-connector-python-1.2.2.tar and MySQL-python-1.2.3.tar
    Install is possible with python setup.py install but if i want use this modules i ran in several error's.

    Anyone here tried to connect a mysql database?

    Thanks,

    Berto
     
  2. koitsu

    koitsu Network Guru Member

    What does this have to do with TomatoUSB?
     
  3. Berto

    Berto Network Newbie Member

    I want to connect my 'smart-meter' (from the local energy company) to the serial port connected to this router. That works so fair. pl2303 serial to USB, insmod the kernel module, and Python can read the serial port.
    But the measuring-data must inserted to a mysql database on a other server. So the Tomato router lives by my 'smart' energy meter and it would be nice if that is possible. No long serial wires, or a Raspberry-PI needed for this simple task. (read out the smart meter and insert the data in the database).
     
  4. JoeDirte

    JoeDirte Serious Server Member

    I can pull my smart meter data from the electric utility's web site. (PG&E here in California)
     
  5. Berto

    Berto Network Newbie Member

    Hi,
    Here in the Netherlands a smart meter has a GPRS interface (wireless data connection over the public phone network) to the energy company. The serial interface gives even so every 10 seconds the measuring counters of the electricity and gas usage.
    So i want to read the serial port and that works. Now transport the data to a MySQL database, because i can't run a MySQL database on a Tomato router i think. But the last step, connecting from my Python script to a MySQL database fails. The build and install of the Python mysql.connector succeed, but if i do a ' import mysql.connector' i get a ugly error.

    # python
    Python 2.7.3 (default, May 27 2014, 15:51:09)
    [GCC 4.6.4] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> import serial
    >>> import mysql.connector
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/opt/lib/python2.7/site-packages/mysql/connector/__init__.py", line 43, in <module>
    import mysql.connector.fabric
    File "/opt/lib/python2.7/site-packages/mysql/connector/fabric/__init__.py", line 42, in <module>
    from .connection import (
    File "/opt/lib/python2.7/site-packages/mysql/connector/fabric/connection.py", line 42, in <module>
    from xmlrpc.client import Fault, ServerProxy, Transport
    ImportError: No module named xmlrpc.client
    >>>

    I don't know if it is possible and done before.
     
  6. JoeDirte

    JoeDirte Serious Server Member

    That sounds interesting. Based on what you posted, it looks like the Python script is trying to call a missing module: ImportError: No module named xmlrpc.client.

    Is there a way to test the connector to the MySQL server? I think you can just telnet to the MySQL server on port 1433 (may be different) to see if the server responds, but to actually get a usable connection, you need xmlrpc.client which is presumably remote procedure call client.

    I don't have any recent experience with Python...

    I did find this:
    https://docs.python.org/2/library/xmlrpclib.html
    Code:
    Note
    The xmlrpclib module has been renamed to xmlrpc.client in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
    I wonder if you're using a Python 3 script with Python 2.7.3 and that's the issue.
     
  7. koitsu

    koitsu Network Guru Member

    I'm still not sure what this has to do with TomatoUSB. Python, to my knowledge, does not come with any of the TomatoUSB firmwares (if it does please correct me and let me know which do!), so either you're using Optware or Entware -- and if you're using Entware there is an official support page + tickets you can make to get things updated or ask about which packages to install etc..

    If you're using Optware I would strongly suggest nuking it and going with Entware instead. Although, to be frank, I do not see an Entware package for MySQLdb (the extension used to provide a MySQL database connection from within Python), so if you're using Entware you could open up a ticket with them and ask for that package. Or if you really insist upon using the official Oracle Python MySQL Connector, then you could ask them to make a package for that.

    P.S. -- Functionally I don't see why XML RPC (nor MySQL for that matter), is needed for something as simple as getting data from a USB-attached device and sticking the results into a flat file or CSV or anything else that's simple. An actual SQL server is total overkill for this (IMO). Embedded devices are a very different beast than desktop or server-side computing.

    MySQL server and client communication does not use XML in any way, it has its own binary format and the main communication medium is either TCP or UNIX domain sockets. So whatever that dependency is (on the part of Oracle, from the sound of it) is ridiculously silly.

    If you really want to use something that's SQL, just to make it easier on yourself, go with SQLite -- it uses flat files (no server/daemon/etc.) and Entware has support for it (package is called python-sqlite3, and you'll probably want the package called sqlite3-cli as well since that has the command-line utils to help manage or tinker with sqlite DBs as flat files).

    Python-related packages on Entware:

    Code:
    root@gw:/tmp/home/root# opkg list | grep -i python
    mako - 0.2.5-1 - Mako is a template library written in Python. It provides a familiar, non-XML syntax which compiles into Python modules for maximum performance. Mako's syntax and API borrows from the best ideas of many others, including Django templates, Cheetah, Myghty, and Genshi. Conceptually, Mako is an embedded Python (i.e. Python Server Page) language, which refines the familiar ideas of componentized layout and inheritance to produce one of the most straightforward and flexible models available, while also maintaining close ties to Python calling and scoping semantics.
    pydnet - 1.11-2 - libdnet is a library of simplified, portable interface to several low-level networking routines. This package contains the python dnet library.
    pyopenssl - 0.10-1 - Python interface to the OpenSSL library.
    python - 2.7.3-2 - Python is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code. . This package contains the full Python install.
    python-bluez - 0.13-1 - Python wrapper for the BlueZ Bluetooth stack
    python-bzip2 - 2.7.3-2 - Python support for Bzip2
    python-chardet - 2.2.1-1 - Chardet guesses the encoding of text files. Includes a command-line script which reports on the encodings of one or more files.
    python-cheetah - 2.4.4-1 - Cheetah is an open source template engine and code generation tool, written in Python. It can be used standalone or combined with other tools and frameworks. Web development is its principle use, but Cheetah is very flexible and is also being used to generate C++ game code, Java, sql, form emails and even Python code.
    python-cjson - 1.0.5-1 - Fast JSON encoder/decoder for Python
    python-cloudprint - 0.11-1 - This is a Python bindings for the cloudprint API, known as pycloudprint. It was written for use with system-config-printer, but can be put to other uses as well.
    python-crypto - 2.6-1 - A collection of both secure hash functions (such as MD5 and SHA), and various encryption algorithms (AES, DES, IDEA, RSA, ElGamal, etc.).
    python-cups - 1.9.66-1 - This is a Python bindings for the CUPS API, known as pycups. It was written for use with system-config-printer, but can be put to other uses as well.
    python-curl - 7.19.0-1 - Python module interface to the cURL library.
    python-django - 1.3.1-1 - Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
    python-doc - 2.7.3-2 - Python interactive documentation
    python-dropbox - 2.0.0-1 - The SDKs contain platform-specific libraries that wrap the raw HTTP calls to the Dropbox API. They are designed to shorten the distance between your application and integrating Dropbox.
    python-expat - 2.7.3-2 - Python support for expat
    python-gdbm - 2.7.3-2 - Python support for gdbm
    python-gevent - 0.13.8-1 - Gevent is a coroutine-based Python networking library that uses greenlet to provide a high-level synchronous API on top of the libevent event loop.
    python-greenlet - 0.4.2-1 - Lightweight in-process concurrent programming.
    python-gzip - 2.7.3-2 - Python support for gzip
    python-imaging-library - 1.1.7-2 - The Python Imaging Library adds image processing capabilities to your Python interpreter. . This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities. . The core image library is designed for fast access to data stored in a few basic pixel formats. It should provide a solid foundation for a general image processing tool.
    python-mini - 2.7.3-2 - Python is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code. . This package contains only a minimal Python install.
    python-ncurses - 2.7.3-2 - Python support for readline
    python-oauth - 1.0.1-1 - Library for OAuth version 1.0a.
    python-onedrive - 14.05.7-1 - Python and command-line interface for Microsoft LiveConnect OneDrive REST API v5.0.
    python-openssl - 2.7.3-2 - Python support for OpenSSL
    python-requests - 2.3.0-1 - Requests allow you to send HTTP/1.1 requests. You can add headers, form data, multipart files, and parameters with simple Python dictionaries, and access the response data in the same way. It's powered by httplib and urllib3, but it does all the hard work and crazy hacks for you.
    python-shutil - 2.7.3-2 - Python support for shutil
    python-smbus - 2013-12-15-1 - This package contain the python bindings for Linux SMBus access through i2c-dev.
    python-socksipychain - 2.0.12-1 - Chaining support for socksipy - a Python SOCKS client module. It provides a socket-like interface that supports connections to any TCP service through the use of a SOCKS4, SOCKS5 or HTTP proxy.
    python-sqlite3 - 2.7.3-2 - Python support for sqlite3
    python-werkzeug - 0.10-20130904-1 - Werkzeug started as simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility modules.
    python2-chardet - 2.0.1-1 - Character encoding auto-detection in Python.
    pyxdg - 0.18-1 - A Python module to deal with freedesktop.org specifications
    pyyaml - 3.11-1 - yaml python bindings
    simplejson - 2.2.1-1 - Simple, fast, extensible JSON encoder/decoder for Python
    twisted - 2.5.0-1 - Python networking engine
    
    MySQL-related packages on Entware:

    Code:
    root@gw:/tmp/home/root# opkg list | grep -i mysql
    asterisk11-mysql - 11.8.1-1 - Asterisk is a complete PBX in software. It provides all of the features you would expect from a PBX and more. Asterisk does voice over IP in three protocols, and can interoperate with almost all standards-based telephony equipment using relatively inexpensive hardware. This package provides support MySQL support in Asterisk.
    asterisk18-mysql - 1.8.27.0-1 - Asterisk is a complete PBX in software. It provides all of the features you would expect from a PBX and more. Asterisk does voice over IP in three protocols, and can interoperate with almost all standards-based telephony equipment using relatively inexpensive hardware. This package provides MySQL support to Asterisk.
    freeradius2-mod-sql-mysql - 2.2.3-3 - MySQL module
    kamailio-mod-db-mysql - 1.5.5-2 - kamailio MySQL database-backend module
    libmysqlclient - 5.1.73-1 - MySQL client library
    libmysqlclient-r - 5.1.73-1 - MySQL client library threadsafe
    lighttpd-mod-mysql_vhost - 1.4.35-1 - Mysql virtual hosting module
    mysql-server - 5.1.73-1 - MySQL Server
    php5-mod-mysql - 5.4.27-1 - MySQL shared module
    php5-mod-mysqli - 5.4.27-1 - MySQL Improved Extension shared module
    php5-mod-pdo-mysql - 5.4.27-1 - PDO driver for MySQL shared module
    snort-mysql - 2.9.5.5-1 - Snort is an open source network intrusion detection and prevention system. It is capable of performing real-time traffic analysis, alerting, blocking and packet logging on IP networks.  It utilizes a combination of protocol analysis and pattern matching in order to detect anomalies, misuse and attacks. This package contains snort with support for logging to a MySQL database.
    strongswan-mod-mysql - 5.1.3-1 - StrongSwan MySQL database interface plugin
    weave-minimal - 1.5-1 - This is a lightweight implementation of Mozilla User API v1.0 and Storage API v1.1 without LDAP, MySQL, Redis etc. overhead.
    
     
    Last edited: Jul 9, 2014

Share This Page