Python escape special characters in password

First of all, using sshpass with the option -p means that you publish your password to anyone on the same machine. It's like putting your house key under your rug.

The error message comes from the shell which is being used to execute the command above (probably BASH). The character ! is interpreted as special character and means "look in the history for the text after me".

The problem is most likely inside of the sshpass script (since you didn't specify shell=True in Popen()). You can try to fix the script by making sure that it uses proper quoting and escaping.

The other solution is to pass env={'SSHPASS': 'test!@#'} to Popen() to set the environment for sshpass as explained in the manpage:

cmd = ['sshpass', '-e', 'ssh', '-o', 'UserKnownHostsFile=/dev/null', ...]
env = os.environ.copy()
env['SSHPASS'] = 'test!@#'
Popen(cmd, env=end)

Note: You should split the "-o key=value" into "-o", "key=value".

Related:

  • Python subprocess/Popen with a modified environment


Escape Characters

To insert characters that are illegal in a string, use an escape character.

An escape character is a backslash \ followed by the character you want to insert.

An example of an illegal character is a double quote inside a string that is surrounded by double quotes:

Example

You will get an error if you use double quotes inside a string that is surrounded by double quotes:

txt = "We are the so-called "Vikings" from the north."

Try it Yourself »

To fix this problem, use the escape character \":

Example

The escape character allows you to use double quotes when you normally would not be allowed:

txt = "We are the so-called \"Vikings\" from the north."

Try it Yourself »

Other escape characters used in Python:

CodeResultTry it
\' Single Quote Try it »
\\ Backslash Try it »
\n New Line Try it »
\r Carriage Return Try it »
\t Tab Try it »
\b Backspace Try it »
\f Form Feed
\ooo Octal value Try it »
\xhh Hex value Try it »



Hello @Henri.GARDON

How did you send the password to the HTTP request? My test password contains the "@", "#","!", etc. but I can manage to send the password to the RDP Auth Service by cover it with a double-quote (" ") like the following example:

python market_price_rdpgw_service_discovery.py --user machine-id --password "<The password with @, #, !, = , ...>" --clientid app_key

java MarketPriceRdpGwServiceDiscovery --user machine-id --password "<The password with @, #, !, = , ...>" --clientid app_key

Author: Pawel Krawczyk

Password special characters is a selection of punctuation characters that are present on standard US keyboard and frequently used in passwords.

CharacterNameUnicode
  Space U+0020
! Exclamation U+0021
Double quote U+0022
# Number sign (hash) U+0023
$ Dollar sign U+0024
% Percent U+0025
& Ampersand U+0026
Single quote U+0027
( Left parenthesis U+0028
) Right parenthesis U+0029
* Asterisk U+002A
+ Plus U+002B
, Comma U+002C
- Minus U+002D
. Full stop U+002E
/ Slash U+002F
: Colon U+003A
; Semicolon U+003B
< Less than U+003C
= Equal sign U+003D
> Greater than U+003E
? Question mark U+003F
@ At sign U+0040
[ Left bracket U+005B
\ Backslash U+005C
] Right bracket U+005D
^ Caret U+005E
_ Underscore U+005F
` Grave accent (backtick) U+0060
{ Left brace U+007B
| Vertical bar U+007C
} Right brace U+007D
~ Tilde U+007E

The same list as string (between double quotes): " !"#$%&'()*+,-./:;<=>[email protected][\]^_`{|}~"

Various operating systems and applications may apply limitations to this set:

  • Oracle Identity Manager and Microsoft Active Directory

How do you escape special characters in Python?

Escape sequences allow you to include special characters in strings. To do this, simply add a backslash ( \ ) before the character you want to escape.

How does Python handle special characters in password?

Simply use \ character before '!' or other special characters. Show activity on this post. Python does not like the special characters.

How do you add a special character to a password in Python?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.

How do you escape characters for password?

To escape \ , $ , ' , and " , use a double quotation mark around the password or use the back slash (\) as the escape character.