Troubleshooting pymssql connection to Azure mssql on linux cloud at pythonanywhere - python

I have not been able to connect to an Azure hosted MSSQL server through pymssql on pythonanywhere.
I have zero previous linux experience, so that is making it harder than it should.
I am trying this sample script provided with the instructions:
host = "123.456.789.012"
username = "yourusername"
password = "yourpassword"
database = "yourdatabasename"
conn = pymssql.connect(host, username, password, database)
cursor = conn.cursor()
But I get this error:
Traceback (most recent call last):
File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10734)
File "_mssql.pyx", line 1902, in _mssql.connect (_mssql.c:21821)
File "_mssql.pyx", line 637, in _mssql.MSSQLConnection.__init__ (_mssql.c:6581)
File "_mssql.pyx", line 1630, in _mssql.maybe_raise_MSSQLDatabaseException (_mssql.c:17524)
_mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (my_server_name.database.windows.net:1433)\n')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pymssql.pyx", line 641, in pymssql.connect (pymssql.c:10824)
pymssql.OperationalError: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (my_server_name.database.windows.net:1433)\n')
I figured it had to do with the TDS Version I am running, which I get to be 4.2 by running tsql -C on bash. Here is the output:
Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /etc/freetds
MS db-lib source compatibility: no
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes
I have tried placing a .freetds.conf file both at home/myuser/ and at home/myuser/my_virtual_env/, but that did not change any behavior. Should I have restarted the bash console or the python console? I did not think so. Here are the file contents:
[global]
tds version = 7.1
[azure]
tds version = 7.4
The other alternative would be to use pyodbc, but that would also involve configuring freetds and then some more, so it looks like there is nothing to be had there.
I should point out that I have pymssql connecting just fine to azure on my win10 machine with the following connection string:
arguments = dict(server=db_server_add, user=db_usn+"#my_server",
password=db_pswd, database=db_name, charset="utf8")
AZURE_ENGINE = create_engine('mssql+pymssql:///', connect_args=arguments)

In Python Anywhere on a free account, it does not allow access to all web addresses. Only addresses from a specific list.
"If you have a paid Python Anywhere plan"
https://help.pythonanywhere.com/pages/MSSQLServer/

Related

Stuck with a MySQLdb SSL connection error in Python2.7

I have to maintain an old website built using Python2.7 that needs to continue working until we've finished creating a completely new version with more modern tools. Now this old website needs access to a remote MySQL database (connection is set up and working correctly), which so far has worked using the following:
import MySQLdb
db = MySQLdb.connect(host=Host,user=User,passwd=Pass,db=DBse)
Now the server has been upgraded from Ubuntu 18.04. to Ubuntu 20.04., and while I managed to install pip and MySQLdb for Python2.7, I now get the following error for the lines above:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 86, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2026, 'SSL connection error: unknown error number')
The SSL connection works fine in Python3 or directly from the command line.
Is there anything I can do to make this work?
Locate and add the line skip_ssl in /etc/mysql/my.cnf ( have tried this on MySQL 8.0.29 , Ubuntu 18)
This was the only thing that worked for me.
[mysqld]
skip_ssl

Paramiko SSHClient.connect(...) fails when packaged with Pyinstaller on Windows

Paramiko's SSHClient.connect(...) method will raise an exception when packaged with Pyinstaller, but will run fine when not packaged.
This occurs using both of the Pyinstaller flags, --onedir and --onefile. I have tried Pyinstaller versions 3.5 and the current latest dev version, 4.0.dev0+ce887b462. --noupx does not have a noticeable effect.
My Paramiko version is 2.6.0.
My current Python version is 2.7.15
My OS is Windows 10 Pro, Version 10.0.17134, Build 17134.
This issue seems to result from Paramiko calling a C function in kernel32.dll, which unexpectedly returns null only when packaged.
Minimal example:
import paramiko
# The nature of this issue unfortunately requires
# a server that responds to SSH connections to test
host = None # Put a valid hostname here
port = 22 # Put a valid port here
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
hostname=host,
port=port,
username=None,
password=None,
timeout=5
)
I expect the Pyinstaller-packaged version to run exactly the same as the unpackaged version -- in this case, to initiate a SSH connection, then fail with an AuthenticationException, since username and password are None:
> virtualenv venv
> venv\Scripts\activate.bat
> pip install pyinstaller==3.5 paramiko==2.6.0
[output truncated for brevity]
> pyinstaller minimal-paramiko-example.py
[output truncated for brevity]
> dist\minimal-paramiko-example\minimal-paramiko-example.exe
Traceback (most recent call last):
File "minimal-paramiko-example.py", line 16, in <module>
timeout=5
File "site-packages\paramiko\client.py", line 446, in connect
File "site-packages\paramiko\client.py", line 691, in _auth
File "site-packages\paramiko\agent.py", line 379, in __init__
File "site-packages\paramiko\agent.py", line 65, in _connect
File "site-packages\paramiko\agent.py", line 82, in _send_message
File "site-packages\paramiko\win_pageant.py", line 129, in send
File "site-packages\paramiko\win_pageant.py", line 96, in _query_pageant
File "site-packages\paramiko\_winapi.py", line 179, in write
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
[16768] Failed to execute script minimal-paramiko-example
> python minimal-paramiko-example.py # Will result in an AuthenticationException, since username and password are None; this is expected and normal
[output truncated for brevity]
This issue is caused by an interaction with PuTTY Pageant. Closing PuTTY Pageant causes this error to not occur. For some reason, it only manifests in the corner case of:
Using Pyinstaller
Running PuTTY Pageant
Failing to satisfy both requirements will not trigger the issue.

Use mysqldb connect ipv6 server in Python

I use mysqldb connect my db server, server'ip is ::1. Code like this:
MySQLdb.connect(host='::1',user='admin',passwd='123456',db='test')
but, I get an error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-i686/egg/MySQLdb/__init__.py", line 81, in Connect
File "build/bdist.linux-i686/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (2005, "Unknown MySQL server host '::1' (1)")
I want to know what should I do to allow mysqldb to support IPv6.
Instead of using ::1 for the host, try use localhost.
If that doesn't work, your system likely does not support IPv6. You can find more information about IPv6 support here.

SSH via python (paramiko) error: "'_EllipticCurvePublicKey' object has no attribute 'verify'"

I am trying to connect to a SSH server using the python library paramiko.
The same code worked on the same computer before, but it started to show a warning when trying to connect. I can connect to the SSH server using the same computer and same users via terminal. The python code also works on other computers connected to the same LAN network.
I also tried to restart both pcs and reinstall paramiko with no success.
Following is the code and the warning message presented.
class SSH:
def __init__(self, ip):
self.ssh = SSHClient()
self.ssh.load_system_host_keys()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(hostname=ip ,username='urs',password='pass')
Warning message:
Unknown exception: '_EllipticCurvePublicKey' object has no attribute 'verify'
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1886, in run
self.kex_engine.parse_next(ptype, m)
File "/usr/local/lib/python3.5/dist-packages/paramiko/kex_ecdh_nist.py", line 47, in parse_next
return self._parse_kexecdh_reply(m)
File "/usr/local/lib/python3.5/dist-packages/paramiko/kex_ecdh_nist.py", line 105, in _parse_kexecdh_reply
self.transport._verify_key(K_S, sig)
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1748, in _verify_key
if not key.verify_ssh_sig(self.H, Message(sig)):
File "/usr/local/lib/python3.5/dist-packages/paramiko/ecdsakey.py", line 216, in verify_ssh_sig
self.verifying_key.verify(
AttributeError: '_EllipticCurvePublicKey' object has no attribute 'verify'
Do anybody have a suggestion about where the problem might be?
I tried to google and looked for the error here,but couldn't find it anywhere.
Thanks in advance!
I had the exact same error as now. I encountered this error with paramiko version 2.3.0. I downgraded to 2.1.2 and the operation i was doing proceeded normally.

Using pymssql to connect to sql server in Ubuntu

I am trying to use pymssql library to connect to the sql server in ubuntu but got the following error. But I tried the same script in the mac and it works.
I think there is a FreeTDS configuration problem in ubuntu but almost tried all of the recommanded methods in the web still no luck so far. Anyone can point me to the right direction?
import pymssql
conn = pymssql.connect(server='blah.database.windows.net',
user='blah',
password='!',
database='database')
cursor = conn.cursor()
cursor.execute('SELECT * FROM table')
data = cursor.fetchall()
print(data)
error:
Traceback (most recent call last):
File "test.py", line 13, in <module>
database='my data base')
File "pymssql.pyx", line 641, in pymssql.connect (pymssql.c:10824)
pymssql.OperationalError: (20002, 'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')

Categories