Writing a connection string when password contains special characters
Backslashes aren't valid escape characters for URL component strings. You need to URL-encode the password portion of the connect string:
from urllib import quote_plus as urlquote
from sqlalchemy.engine import create_engine
engine = create_engine('postgres://user:%s@host/database' % urlquote('badpass'))
If you look at the implementation of the class used in SQLAlchemy to represent database connection URLs (in sqlalchemy/engine/url.py
), you can see that they use the same method to escape passwords when converting the URL instances into strings, and that the parsing code uses the complementary urllib.unquote_plus
function to extract the password from a connection string.
Password with special characters in connectionString
In this example I see two things:
- A
&
in xml should be escaped with&
(Good explanation in this answer) - A
;
in a connection string, you should wrap the password in single quotes
So this should work for you:
<connectionStrings>
<add name="CRM365" connectionString="AuthType=AD;Url=http://crm.xxx.com/CRM365; Domain=test; Username=test; Password='T,jL4O&vc%t;30'" />
</connectionStrings>
Edit (just tried it for myself):
Additionally, another variant is to use escaped double quotes:
<connectionStrings>
<add name="CRM365" connectionString="AuthType=AD;Url=http://crm.xxx.com/CRM365; Domain=test; Username=test; Password="T,jL4O&vc%t;30"" />
</connectionStrings>
Summary:
Use either password='T,jL4O&vc%t;30';
or password="T,jL4O&vc%t;30";
Access denied for user when password contains special characters
If any of the constituent parts of the database URL might contain special characters that would potentially confuse the URL parser it is best to have sqlalchemy.engine.url.URL
construct the URL for you. For example:
import sqlalchemy as sa
connection_url = sa.engine.url.URL(drivername="mysql+pymysql",
username="db_user",
password="my:password",
host="localhost",
port="3307",
database="mydb",
)
print(connection_url)
# mysql+pymysql://db_user:my%3Apassword@localhost:3307/mydb
Notice that the colon character in the password is percent-encoded as %3A
.
mongodb shell login - password contains special characters like -(hyphen) and '(single quote)
The manual for the deprecated mongo
command makes it clear that the preferred way to pass a connection string is as a URI. https://docs.mongodb.com/manual/reference/connection-string/ makes it clear that usernames and passwords can be passed as part of that URI.
mongo mongodb://username:-8B%27df5%3D@hostname/admin
%27
is the URL-escaping version of '
%3D
is the URL-escaping version of =
Python's urllib.quote()
is one of the many ways you can look up these mappings yourself.
SQLAlchemy ValueError for slash in password for create_engine()
Slashes aren't valid characters for URL component strings. You need to URL-encode the password portion of the connect string:
from urllib import quote_plus as urlquote
from sqlalchemy.engine import create_engine
engineString = 'postgresql://wberg:%s@localhost/mydatabase' % urlquote('pass/word')
engine = create_engine(engineString)
Related Topics
How to Efficiently Parse Fixed Width Files
Why Do I Get a Syntaxerror for a Unicode Escape in My File Path
How to Compare Version Numbers in Python
How to Access Command Line Arguments
Quoting Backslashes in Python String Literals
Pandas: Adding New Column to Dataframe Which Is a Copy of the Index Column
Efficiently Using Multiple Numpy Slices for Random Image Cropping
Python Subprocess Get Children's Output to File and Terminal
Getting the Class Name of an Instance
What Is a Cross-Platform Way to Get the Home Directory
How to Use a Dll File from Python
Rotate Axis Text in Python Matplotlib
How to Get a String After a Specific Substring