I have a string of unprinted characters. I need to send it to the server. The problem is that in the "raw" form you will not convey the drain (there may be special characters in it). I do urlquote . But urlquote does not work with Unicode, and it replaces all non-printed characters, as a result of which a distorted string comes to the server. How can I make the initial line come to the server?

I want to do this: take a line and convert it so that the new line will contain character codes, for example, the new line may look like this - "\xfa\xdd\x99..." . The question is how to get such a string? Tried the packet struct.pack , did not work.

I will add. The initial task is to transfer buffer bytes to the server, which I convert to a string and transmit in a GET request. Perhaps there is an alternative solution.

    2 answers 2

    base64encode/base64decode ?

    • Like base64 was created for such cases? - Stas Litvinenko
    • Yeah, cool stuff. Only one thing - I don't have python / python. Python is only on one side. As I understood on the other side, another codec is used, because I cannot get an identical string on the server. Maybe other suggestions? - Nicolas Chabanovsky
    • In general, base64 is a standard. And the standards are the same everywhere, regardless of the programming language. - Alex Kapustin
    • As far as I remember in base64 with only two characters there can be problems. 63 and 64 characters. But Python allows you to set them manually. - Stas Litvinenko
    • one
      python -c 'import base64; print base64.b64encode ("\ xfa \ xdd \ x99 ..."); ' - Alex Kapustin

    Try bad_line.encode ('unicode-escape', 'replace') I'm not quite sure about the 'replace' key. Need to try.