0

I am creating a script which will fetch the data from mysql server and decrypt the text using Jasypt which is a java library to encrpyt/decrypt text. So I have to do this using Jython.

I am able to import java class from Jasypt library. I am also able to import mysql.connector into Jython script. But script is not able to make connection to mysql server.

Here is the code which I am trying

import sys
SYSPATH = ['/usr/local/lib/python2.7/site-packages', '/data/packages/jasypt-1.9.2.jar']
[sys.path.append(p) for p in SYSPATH]
import mysql.connector

# adding this to avoid "AttributeError: socket.SOL_TCP does not exist" listed at https://github.com/pika/pika/issues/67
import socket
socket.SOL_TCP = socket.IPPROTO_TCP

# This line is giving error "TypeError: unpack_from(): 2nd arg can't be coerced to String"
con = mysql.connector.connect(user='username', password='password', host='192.168.123.123', database='users')

last line is gving me error

 File "2017-04-05-get-bank-sms.py", line 11, in createUserIdUuidMap
   con = mysql.connector.connect(user='username', password='password',    host='192.168.123.123', database='users')
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/__init__.py", line 179, in connect
   return MySQLConnection(*args, **kwargs)
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 95, in __init__
   self.connect(**kwargs)
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/abstracts.py", line 719, in connect
   self._open_connection()
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 207, in _open_connection
   self._do_handshake()
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 99, in _do_handshake
   packet = self._socket.recv()
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/network.py", line 235, in recv_plain
   payload_len = struct.unpack_from(
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/network.py", line 235, in recv_plain
   payload_len = struct.unpack_from(
TypeError: unpack_from(): 2nd arg can't be coerced to String

Is it possible what I am trying?

2
  • I doubt that 192.168.321.123 is a valid IP-Address... Commented Apr 6, 2017 at 14:14
  • 1
    ohh ... that was not real IP for posting here. I have corrected it to be valid IP. Thanks :) Commented Apr 6, 2017 at 14:31

1 Answer 1

1

Based on the other related questions it appears that the most common approach is to use "MySQL Connector/J" (the JDBC driver) instead of "MySQL Connector/Python". I can confirm that this Jython code works ...

from com.ziclix.python.sql import zxJDBC

connectionUrl = "jdbc:mysql://localhost:3307"
cnxn = zxJDBC.connect(
        connectionUrl,
        "root",
        "mypassword",
        "com.mysql.jdbc.Driver")
crsr = cnxn.cursor()
crsr.execute("SHOW DATABASES")
rows = crsr.fetchall()
print(rows)

... when I run it using the following batch file:

SET CLASSPATH=C:\Users\gord\.m2\repository\mysql\mysql-connector-java\5.1.41\mysql-connector-java-5.1.41.jar
C:\jython2.7.0\bin\jython mysql_test.py
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.