WebFaction
Community site: login faq

When I call

server.system(self.session_id,  cmd)

with utf-8 encoded cmd variable (some string have accented characters like ?áéú?üí)

I get

Fault: <Fault 1: "'ascii' codec can't encode character u'\\xe1' in position 428: ordinal not in range(128)">

asked 11 Jan '11, 14:47

Akasha
11711
accept rate: 0%

Please Provide all the lines of code before that one that would make it work. That is not enough info for us to assume the context.

(11 Jan '11, 16:11) johns

to reproduce, run: server.system(session_id, 'echo "Kovács Balázs"')

as you can see, the context is rather general I would like to run some custom sql using the api, like 'UPDATE users set name="Kovács Balázs" where id=%d'

I've tried to write the sql into a file, and run that file directly, but write_file failed too. With the same error.

server.system(session_id, u'echo "Kovács Balázs"') gives a similar error

(11 Jan '11, 19:28) Akasha

Assuming you are using the Webfaction API, XML-RPC only supports ASCII, which is why this is failing. You will have to write a local shell script or use MySQL directly to do the operation.

permanent link

answered 11 Jan '11, 20:50

johns
5.4k412
accept rate: 23%

According to effbot, XML-RPC supports everything that XML supports. Anyway, I've solved the encoding problem by encoding in b64, transferring, and decoding on the server.

    from base64 import b64encode
    non_ascii_text = 'A crazy, non-ascii text, ?áéú?üöy'
    server.write_file(self.session_id, cfg_sql, b64encode(' '.join(non_ascii_text)), 'w')
    cmd = """%(app)s %(file)s;""" % {'app': os.path.join(self.home, self.app_path, 'decode64.py'), 'file': cfg_sql}
    cmd += """my_initial_command < cfg_sql"""
    server.system(self.session_id,  cmd)

and decode64.py is:

#!/usr/bin/env python
import os, base64, sys
appdir = os.path.normpath(os.path.dirname(__file__))
os.chdir(appdir)
def decode(filename):
    txt = base64.b64decode(open(filename, 'r').read())
    open(filename, 'w').write(txt)

if __name__ == '__main__':
    decode(sys.argv[1])
permanent link

answered 12 Jan '11, 02:56

Akasha
11711
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×76
×3
×3
×2

question asked: 11 Jan '11, 14:47

question was seen: 4,598 times

last updated: 12 Jan '11, 02:56

                              
WEBFACTION
REACH US
SUPPORT
LEGAL
© COPYRIGHT 2003-2021 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM