Skip to content

Instantly share code, notes, and snippets.

@k4ml
Last active April 12, 2023 20:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save k4ml/5889346 to your computer and use it in GitHub Desktop.
Save k4ml/5889346 to your computer and use it in GitHub Desktop.
Python client for Webfaction API
# API Reference¶
The WebFaction [XML-RPC](http://en.wikipedia.org/wiki/XML-RPC) API provides
methods to make many account tasks scriptable. This documentation is a
complete reference to all of the possible API methods.
Please note that XML-RPC parameters are positional (order matters), and many
parameters are required. Parameters may only be omitted if omitted parameters
have default values and follow all other parameters to which you have supplied
a value.
## General¶
`login`¶
Parameters:
* **username** (_string_) - a valid WebFaction control panel username
* **password** (_string_) - a valid WebFaction control panel user's password
* **machine** (_string_) - the case-sensitive machine name (for example, `Web55`); optional for accounts with only one machine
Log in a user and return credentials required to make requests for that user.
The method returns a session ID string and a struct containing following key-
value pairs:
_id_
account ID
_username_
username
_home_
home directory path
_web_server_
Web server associated with the logged in account (for example, `Web55`)
_mail_server_
mail server associated with the logged in account (for example, `Mailbox2`)
Note
The session ID is required for all subsequent API calls.
`list_disk_usage`¶
Parameters:
**session_id** - session ID returned by `login`
List disk space usage statistics about your account (similar to usage
statistics shown [on the control
panel](https://my.webfaction.com/disk_usage)). The method returns a struct
containing the following members:
`home_directories`
A list of structs with details for each home directory associated with the
account. Each struct contains the following members:
`last_reading`
The date and time of the last recording of the home directory's size
`machine`
The server name (for example, `Web300`)
`name`
The username
`size`
The disk usage in kilobytes
`mailboxes`
A list of structs with details for each mailbox associated with the account.
Each struct contains the following members:
`last_reading`
The date and time of the last recording of the mailbox's size
`name`
The mailbox name
`size`
The disk usage in kilobytes
`mysql_databases`
A list of structs with details for each MySQL database associated with the
account. Each struct contains the following members:
`last_reading`
The date and time of the last recording of the database's size
`name`
The database name
`size`
The disk usage in kilobytes
`postgresql_databases`
A list of structs with details for each PostgreSQL database associated with
the account. Each struct contains the following members:
`last_reading`
The date and time of the last recording of the database's size
`name`
The database name
`size`
The disk usage in kilobytes
`total`
The account's total disk usage in kilobytes
`quota`
The account's total disk allotment in kilobytes
`percentage`
The account's total disk usage as a percentage of the quota (for example, an account using 3.1 GB of 100 GB would use `3.1` percent of its quota)
## Email¶
### Mailboxes¶
`change_mailbox_password`¶
Parameters:
* **session_id** - session ID returned by `login`
* **mailbox** (_string_) - a valid mailbox name
* **password** (_string_) - the new mailbox password
Change a mailbox password.
See also
See [Strengthening Passwords](http://docs.webfaction.com/user-
guide/passwords.html) for important information about choosing passwords.
`create_mailbox`¶
Parameters:
* **session_id** - session ID returned by `login`
* **mailbox** (_string_) - mailbox name
* **enable_spam_protection** (_boolean_) - whether spam protection is enabled for the mailbox (optional, default: true)
* **discard_spam** (_boolean_) - whether spam messages received by the new mailbox are discarded (optional, default: false)
* **spam_redirect_folder** (_string_) - name of the IMAP folder where messages identified as spam are stored (optional, default: an empty string)
* **use_manual_procmailrc** (_boolean_) - whether to use manual procmailrc rules as specified by the `manual_procmailrc` parameter (optional, default: false)
* **manual_procmailrc** (_string_) - the procmailrc rules for the mailbox (optional, default: an empty string)
Warning
If `discard_spam` is true, messages misidentified as spam--false positives--
may be lost permanently.
Create a mailbox and return a struct containing the following key-value pairs:
_id_
mailbox ID
_name_
mailbox name
_enable_spam_protection_
name of the folder where messages identified as spam are stored
_password_
a randomly generated password
_discard_spam_
a boolean indicating whether spam emails are be discarded
_spam_redirect_folder_
name of the IMAP folder where messages identified as spam are stored
_use_manual_procmailrc_
a boolean indicating whether manual procmailrc rules are enabled
_manual_procmailrc_
a string containing manual procmailrc rules
See also
`update_mailbox`
`delete_mailbox`¶
Parameters:
* **session_id** - session ID returned by `login`
* **mailbox** (_string_) - mailbox name
Delete a mailbox.
`list_mailboxes`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about the account's mailboxes. The method returns an array of
structs with the following key-value pairs:
_id_
mailbox ID
_name_
mailbox name
_enable_spam_protection_
name of the folder where messages identified as spam are stored
_password_
a randomly generated password
_discard_spam_
a boolean indicating whether spam emails are be discarded
_spam_redirect_folder_
name of the IMAP folder where messages identified as spam are stored
_use_manual_procmailrc_
a boolean indicating whether manual procmailrc rules are enabled
_manual_procmailrc_
a string containing manual procmailrc rules
`update_mailbox`¶
Parameters:
* **session_id** - session ID returned by `login`
* **mailbox** (_string_) - mailbox name
* **enable_spam_protection** (_boolean_) - whether spam protection is enabled for the mailbox (optional, default: true)
* **discard_spam** (_boolean_) - whether spam messages received by the new mailbox are discarded (optional, default: false)
* **spam_redirect_folder** (_string_) - name of the IMAP folder where messages identified as spam are stored (optional, default: an empty string)
* **use_manual_procmailrc** (_boolean_) - whether to use manual procmailrc rules as specified by the `manual_procmailrc` parameter (optional, default: false)
* **manual_procmailrc** (_string_) - the procmailrc rules for the mailbox (optional, default: an empty string)
Warning
If `discard_spam` is true, messages misidentified as spam--false positives--
may be lost permanently.
Change the details of an existing mailbox. The mailbox must exist before
calling the method. The method returns a struct containing the following key-
value pairs:
_id_
mailbox ID
_name_
mailbox name
_enable_spam_protection_
name of the folder where messages identified as spam are stored
_password_
a randomly generated password
_discard_spam_
a boolean indicating whether spam emails are be discarded
_spam_redirect_folder_
name of the IMAP folder where messages identified as spam are stored
_use_manual_procmailrc_
a boolean indicating whether manual procmailrc rules are enabled
_manual_procmailrc_
a string containing manual procmailrc rules
See also
`create_mailbox`
### Addresses¶
`create_email`¶
Parameters:
* **session_id** - session ID returned by `login`
* **email_address** (_string_) - an email address (for example, `name@example.com`)
* **targets** (_string_) - names of destination mailboxes or addresses, separated by commas
* **autoresponder_on** (_boolean_) - whether an autoresponder is enabled for the address (optional, default: false)
* **autoresponder_subject** (_string_) - subject line of the autoresponder message (optional, default: an empty string)
* **autoresponder_message** (_string_) - body of the autoresponder message (optional, default: an empty string)
* **autoresponder_from** (_string_) - originating address of the autoresponder message (optional, default: an empty string)
* **script_machine** (_string_) - a machine name for specifying a path to a script (optional, default: an empty string)
* **script_path** (_string_) - an absolute path to a script; see _[Sending Mail to a Script_](http://docs.webfaction.com/user-guide/email.html#email-sending-mail-to-a-script) for details (optional, default: an empty string)
Create an email address which delivers to the specified mailboxes.
If _autoresponder_on_ is true, then an autoresponder subject, message, and
from address may be specified.
See also
`update_email`
`delete_email`¶
Parameters:
* **session_id** - session ID returned by `login`
* **email_address** (_string_) - an email address (for example, `name@example.com`)
Delete an email address.
`list_emails`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about the account's email addresses. The method returns an
array of structs with the following key-value pairs:
_id_
email ID
_email_address_
email address
_targets_
mailboxes or email addresses to which the address is set to deliver
_autoresponder_on_
a boolean indicating whether an autoresponder is enabled for the address
_autoresponder_subject_
the autoresponder subject line (if applicable)
_autoresponder_message_
the autoresponder message body (if applicable)
_autoresponder_from_
the autoresponder from address (if applicable)
`update_email`¶
Parameters:
* **session_id** - session ID returned by `login`
* **email_address** (_string_) - an email address (for example, `name@example.com`)
* **targets** (_array_) - names of destination mailboxes or addresses
* **autoresponder_on** (_boolean_) - whether an autoresponder is enabled for the address (optional, default: false)
* **autoresponder_subject** (_string_) - subject line of the autoresponder message (optional, default: an empty string)
* **autoresponder_message** (_string_) - body of the autoresponder message (optional, default: an empty string)
* **autoresponder_from** (_string_) - originating address of the autoresponder message (optional, default: an empty string)
* **script_machine** (_string_) - a machine name for specifying a path to a script (optional, default: an empty string)
* **script_path** (_string_) - an absolute path to a script; see _[Sending Mail to a Script_](http://docs.webfaction.com/user-guide/email.html#email-sending-mail-to-a-script) for details (optional, default: an empty string)
Change the details of an existing email address. The email address must exist
before calling the method. The method returns a struct with the following key-
value pairs:
_id_
email ID
_email_address_
email address
_targets_
mailboxes or email addresses to which the address is set to deliver
See also
`create_email`
## Websites and Domains¶
`create_domain`¶
Parameters:
* **session_id** - session ID returned by `login`
* **domain** (_string_) - a domain name in the form of `example.com`
* **subdomain** (_string_) - each additional parameter provided after `domain`: a subdomain name of `domain`
Create a domain entry. If `domain` has already been created, you may supply
additional parameters to add subdomains. For example, if `example.com` already
exists, `create_domain` may be called with four parameters-- a session ID,
`example.com`, `www`, `private`--to create `www.example.com` and
`private.example.com`.
**Example**: Create a domain entry for `widgetcompany.example` using Python:
>>> import xmlrpclib
>>> server = xmlrpclib.ServerProxy('https://api.webfaction.com/')
>>> session_id, account = server.login('widgetsco', 'widgetsrock')
>>> server.create_domain(session_id, 'widgetcompany.example', 'www', 'design')
{'domain': 'widgetcompany.example',
'id': 47255,
'subdomains': ['www', 'design']}
`create_website`¶
Parameters:
* **session_id** - session ID returned by `login`
* **website_name** (_string_) - the name of the new website entry
* **ip** (_string_) - IP address of the server where the entry resides
* **https** (_boolean_) - whether the website entry should use a secure connection
* **subdomains** (_array_) - an array of strings of (sub)domains to be associated with the website entry
* **site_apps** (_array_) - each additional parameter provided after `subdomains`: an array containing a valid application name (a string) and a URL path (a string)
Create a new website entry. Applications may be added to the website entry
with additional parameters supplied after `subdomains`. The additional
parameters must be arrays containing two elements: a valid application name
and a path (for example, `'htdocs'` and `'/'`).
**Example:** Create a website entry for `widgetcompany.example`'s new Django project over HTTPS using Python:
>>> import xmlrpclib
>>> server = xmlrpclib.ServerProxy('https://api.webfaction.com/')
>>> session_id, account = server.login('widgetsco', 'widgetsrock')
>>> server.create_website(session_id,
... 'widgets_on_the_web',
... '174.133.82.194',
... True,
... ['widgetcompany.example', 'www.widgetcompany.example'],
... ['django', '/'])
{'https': True,
'id': 67074,
'ip': '174.133.82.194',
'name': 'widgets_on_the_web',
'site_apps': [['django', '/']],
'subdomains': ['widgetcompany.example', 'www.widgetcompany.example']}
`delete_domain`¶
Parameters:
* **session_id** - session ID returned by `login`
* **domain** (_string_) - name of the domain to be deleted or the parent domain of the subdomains to be deleted
* **subdomains** (_string_) - each additional parameter provided after `domain`: subdomains of `domain` to be deleted
Delete a domain record or subdomain records. Subdomains of a domain may be
deleted by supplying additional parameters after `domain`. If any subdomains
are provided, only subdomains are deleted and the parent domain remains.
`delete_website`¶
Parameters:
* **session_id** - session ID returned by `login`
* **website_name** (_string_) - name of website to be deleted
* **ip** (_string_) - IP address of the server where the website resides
* **https** (_boolean_) - whether the website uses a secure connection (optional, default: false)
Delete a website entry.
`list_bandwidth_usage`¶
Parameters:
**session_id** - session ID returned by `login`
List bandwidth usage statistics for your websites (similar to usage statistics
shown [on the control panel](https://my.webfaction.com/bandwidth_usage)). The
method returns a struct containing two members:
`daily`: A struct containing members named for the dates for the past two
weeks (for example, `2014-02-15`, `2014-02-14`, `2014-02-13` and so on). The
value of each dated member is a struct containing members named for each
domain associated with the account (for example, `example.com`,
`www.example.com`, `somedomain.example`, `www.somedomain.example` and so on).
The value of each domain name member is the bandwidth usage for that domain
during that day in kilobytes.
`monthly`: A struct containing members named for the months for the past year
(for example, `2014-02`, `2014-01`, `2013-12` and so on). The value of each
month member is a struct containing members named for each domain associated
with the account (for example, `example.com`, `www.example.com`,
`somedomain.example`, `www.somedomain.example` and so on). The value of each
domain name member is the bandwidth usage for that domain during that month in
kilobytes.
Overall, the struct resembles this outline:
* `daily`
* today
* `www.example.com`: `1024`
* `example.com`: `512`
* …
* yesterday
* …
* two weeks ago
* `monthly`
* this month
* `www.example.com`: `2048`
* `example.com`: `1024`
* …
* last month
* …
* a year ago
`list_domains`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about the account's domains. The method returns an array of
structs with the following key-value pairs:
_id_
domain ID
_domain_
domain (for example, `example.com`)
_subdomains_
array of subdomains for the domain
`list_websites`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about the account's websites. The method returns an array of
structs with the following key-value pairs:
_id_
website ID
_name_
website name
_ip_
website IP address
_https_
whether the website is served over HTTPS
_subdomains_
array of website's subdomains
_website_apps_
array of the website's apps and their URL paths; each item in the array is a two-item array, containing an application name and URL path
`update_website`¶
Parameters:
* **session_id** - session ID returned by `login`
* **website_name** (_string_) - the name of the new website entry
* **ip** (_string_) - IP address of the server where the entry resides
* **https** (_boolean_) - whether the website entry should use a secure connection
* **subdomains** (_array_) - an array of strings of (sub)domains to be associated with the website entry
* **site_apps** (_array_) - each additional parameter provided after `subdomains`: an array containing a valid application name (a string) and a URL path (a string)
Update a website entry. Applications may be added to the website entry with
additional parameters supplied after `subdomains`. The additional parameters
must be arrays containing two elements: a valid application name and a path
(for example, `'htdocs'` and `'/'`).
**Example:** Update a website entry for `widgetcompany.example`'s new Django project over HTTPS using Python:
>>> import xmlrpclib
>>> server = xmlrpclib.ServerProxy('https://api.webfaction.com/')
>>> session_id, account = server.login('widgetsco', 'widgetsrock')
>>> server.update_website(session_id,
... 'widgets_on_the_web',
... '174.133.82.195',
... True,
... ['widgetcompany.example', 'dev.widgetcompany.example'],
... ('django', '/'), ('wordpress', '/blog'))
{'https': True,
'id': 67074,
'ip': '174.133.82.195',
'name': 'widgets_on_the_web',
'site_apps': [['django', '/'], ['wordpress', '/blog']],
'subdomains': ['widgetcompany.example', 'dev.widgetcompany.example']}
## Applications¶
`create_app`¶
Parameters:
* **session_id** - session ID returned by `login`
* **name** (_string_) - name of the application
* **type** (_string_) - type of the application
* **autostart** (_boolean_) - whether the app should restart with an `autostart.cgi` script (optional, default: false)
* **extra_info** (_string_) - additional information required by the application; if _extra_info_ is not required or used by the application, it is ignored (optional, default: an empty string)
* **open_port** (_boolean_) - for applications that listen on a port, whether the port should be open on shared and dedicated IP addresses (optional, default: false)
Create a new application.
See also
For a complete list of application types, see _[Application Types_](apps.html
#application-types).
`delete_app`¶
Parameters:
* **session_id** - session ID returned by `login`
* **name** (_string_) - name of the application
Delete an application.
`list_apps`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about the account's applications. The method returns an array
of structs with the following key-value pairs:
_id_
app ID
_name_
app name
_type_
app type
_autostart_
whether the app uses autostart
_port_
port number if the app listens on a port, otherwise is `0`
_open_port_
for applications that listen on a port, whether the port is open on shared and dedicated IP addresses (`True` for open ports, `False` for closed ports, or for applications that do not listen to a port)
_extra_info_
extra info for the app if any
_machine_
name of the machine where the app resides
`list_app_types`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about available app types. The method returns an array of
structs with the following key-value pairs:
_description_
description of the application type
_name_
short identifier for the application type
_autostart_
`applicable` or an empty string, indicating whether the application uses an `autostart.cgi` file
_extra_info_
description of any additional information required by the application installer's _extra_info_ field
See also
`create_app`
## Cron¶
`create_cronjob`¶
Parameters:
* **session_id** - session ID returned by `login`
* **line** (_string_) - crontab line to be added
Create a new cron job.
See also
For more information about the cron syntax, please see the Wikipedia entry on
[cron](http://en.wikipedia.org/wiki/Cron#crontab_syntax).
`delete_cronjob`¶
Parameters:
* **session_id** - session ID returned by `login`
* **line** (_string_) - crontab line to be removed
Remove an existing cron job.
## DNS¶
`create_dns_override`¶
Parameters:
* **session_id** - session ID returned by `login`
* **domain** (_string_) - domain name to be overridden (for example, `sub.example.com`)
* **a_ip** (_string_) - A IP address (optional, default: an empty string)
* **cname** (_string_) - CNAME record (optional, default: an empty string)
* **mx_name** (_string_) - Mail exchanger record host name (optional, default: an empty string)
* **mx_priority** (_string_) - Mail exchanger record priority (optional, default: an empty string)
* **spf_record** (_string_) - TXT record (optional, default: an empty string)
* **aaaa_ip** (_string_) - An IPv6 address (optional, default: an empty string)
Create DNS records and return an array of the new records (as in the form of
`list_dns_overrides`).
`delete_dns_override`¶
Parameters:
* **session_id** - session ID returned by `login`
* **domain** (_string_) - domain name to be overridden (for example, `sub.example.com`)
* **a_ip** (_string_) - A IP address (optional, default: an empty string)
* **cname** (_string_) - CNAME record (optional, default: an empty string)
* **mx_name** (_string_) - Mail exchanger record host name (optional, default: an empty string)
* **mx_priority** (_string_) - Mail exchanger record priority (optional, default: an empty string)
* **spf_record** (_string_) - TXT record (optional, default: an empty string)
* **aaaa_ip** (_string_) - An IPv6 address (optional, default: an empty string)
Delete DNS records and return an array of the deleted records (as in the form
of `list_dns_overrides`).
`list_dns_overrides`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about the account's DNS overrides. The method returns an array
of structs with the following key-value pairs:
_id_
domain ID
_domain_
domain name to be overridden (for example, `sub.example.com`)
_a_ip_
A IP address
_aaaa_ip_
AAAA IP address (for IPv6)
_cname_
CNAME record
_mx_name_
Mail exchanger record host name
_mx_priority_
Mail exchanger record priority
_spf_record_
TXT record
_srv_record_
Service record
## Databases¶
`change_db_user_password`¶
Parameters:
* **session_id** - session ID returned by `login`
* **username** (_string_) - a databse user's username
* **password** (_string_) - the new password
* **db_type** (_string_) - the database type, either `mysql` or `postgresql`
Change a database user's password. The method returns a struct containing the
following key-value pairs:
_username_
database username
_machine_
database machine name
_db_type_
database type (MySQL or PostgreSQL)
_database_
database name
See also
See [Strengthening Passwords](http://docs.webfaction.com/user-
guide/passwords.html) for important information about choosing passwords.
`create_db`¶
Parameters:
* **session_id** - session ID returned by `login`
* **name** (_string_) - database name
* **db_type** (_string_) - the database type, either `mysql` or `postgresql`
* **password** (_string_) - password for the default database user
Create a database.
Note
MySQL database names may not exceed 16 characters.
See also
See [Strengthening Passwords](http://docs.webfaction.com/user-
guide/passwords.html) for important information about choosing passwords.
`create_db_user`¶
Parameters:
* **session_id** - session ID returned by `login`
* **username** (_string_) - the new database user's username
* **password** (_string_) - the new database user's password
* **db_type** (_string_) - the database type, either `mysql` or `postgresql`
Create a database user. The method returns a struct with the following key-
value pairs:
_machine_
machine name
_username_
database username
_db_type_
database type (MySQL or PostgreSQL)
See also
See [Strengthening Passwords](http://docs.webfaction.com/user-
guide/passwords.html) for important information about choosing passwords.
`delete_db`¶
Parameters:
* **session_id** - session ID returned by `login`
* **name** (_string_) - database name
* **db_type** (_string_) - the database type, either `mysql` or `postgresql`
Delete a database.
`delete_db_user`¶
Parameters:
* **session_id** - session ID returned by `login`
* **username** (_string_) - the database user's username
* **db_type** (strings `mysql` or `postgresql`) - the database type
Delete a database user. The method returns a struct with the following key-
value pairs:
_machine_
machine name
_username_
database username
_db_type_
database type (MySQL or PostgreSQL)
`enable_addon`¶
Parameters:
* **session_id** - session ID returned by `login`
* **database** (_string_) - a database name
* **db_type** (_string_) - the database type (always use `postgresql`)
* **addon** (_string_) - the addon to enable (`tsearch` or `postgis`)
Enable a database addon. The method returns a struct with the following key-
value pairs:
_machine_
machine name
_db_type_
database type (always PostgreSQL)
_addon_
addon enabled
_db_type_
database type (MySQL or PostgreSQL)
Note
This method applies to PostgreSQL databases only.
`grant_db_permissions`¶
Parameters:
* **session_id** - session ID returned by `login`
* **username** (_string_) - a databse user's username
* **database** (_string_) - a database name
* **db_type** (_string_) - the database type (`mysql` or `postgresql`)
Grant full database permissions to a user with respect to a database. The
method returns a struct with the following key-value pairs:
_machine_
machine name
_username_
database username
_db_type_
database type (MySQL or PostgreSQL)
_database_
database name
`list_dbs`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about the account's databases. The method returns an array of
structs with the following key-value pairs:
_id_
database ID
_name_
database name
_db_type_
database type (MySQL or PostgreSQL)
_machine_
machine name
`list_db_users`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about the account's database users. The method returns an
array of structs with the following key-value pairs:
_machine_
machine name
_username_
database username
_db_type_
database type (MySQL or PostgreSQL)
`make_user_owner_of_db`¶
Parameters:
* **session_id** - session ID returned by `login`
* **username** (_string_) - a databse user's username
* **database** (_string_) - a database name
* **db_type** (_string_) - the database type (`mysql` or `postgresql`)
Assign ownership of a database to a user. The method returns a struct with the
following key-value pairs:
_machine_
machine name
_username_
database username
_db_type_
database type (MySQL or PostgreSQL)
_database_
database name
`revoke_db_permissions`¶
Parameters:
* **session_id** - session ID returned by `login`
* **username** (_string_) - a databse user's username
* **database** (_string_) - a database name
* **db_type** (_string_) - the database type (`mysql` or `postgresql`)
Revoke a user's permissions with respect to a database. The method returns a
struct with the following key-value pairs:
_machine_
machine name
_username_
database username
_db_type_
database type (MySQL or PostgreSQL)
_database_
database name
## Files¶
`replace_in_file`¶
Parameters:
* **session_id** - session ID returned by `login`
* **filename** (_string_) - path to file from the user's home directory
* **changes** (_array_) - each additional parameter provided after `filename`: an array of two strings, where the first is the text to be replaced and the second is the replacement text
Find and replace strings in a file in the users's home directory tree.
Any parameters after `filename` must be arrays containing a pair of strings,
where the first is the string to be replaced and the second is the replacement
text.
**Example:** Find all appearances of the word "eggs" in a file in the user's home directory and replace them with the word "spam" using Python:
$ cat myfile.txt
eggs, spam, spam, and spam.
spam, spam, spam, and eggs.
>>> import xmlrpclib
>>> server = xmlrpclib.ServerProxy('https://api.webfaction.com/')
>>> session_id, account = server.login('widgetsco', 'widgetsrock')
>>> server.replace_in_file(session_id, 'myfile.txt', ('eggs', 'spam'))
''
>>> exit()
$ cat myfile.txt
spam, spam, spam, and spam.
spam, spam, spam, and spam.
`replace_in_file`
`write_file`¶
Parameters:
* **session_id** - session ID returned by `login`
* **filename** (_string_) - path to file to be written from the user's home directory
* **str** (_string_) - string to be written
* **mode** (_string_) - write mode (optional, default: `wb`)
Write a string to a file in the user's home directory tree.
Note
Commonly, the write mode is `w` for plain text and `wb` for binaries. `a` and
`ab` can be used to append to files.
See also
For more information about write modes, please see
[open()](http://docs.python.org/library/functions.html#open).
## Shell Users¶
`change_user_password`¶
Parameters:
* **session_id** - session ID returned by `login`
* **username** (_string_) - username
* **password** (_string_) - a new passowrd
Change a shell user's password.
See also
See [Strengthening Passwords](http://docs.webfaction.com/user-
guide/passwords.html) for important information about choosing passwords.
`create_user`¶
Parameters:
* **session_id** - session ID returned by `login`
* **username** (_string_) - username
* **shell** (_string_) - the user's command line interpreter; one of `none`, `bash`, `sh`, `ksh`, `csh`, or `tcsh`.
* **groups** (_array_) - extra permission groups of which the new user is to be a member
Create a new shell user. If `shell` is `none`, the user has FTP access only.
All users are automatically a member of their own group; do not include the
user's own group in `groups`. Use an empty array to specify no extra groups.
`delete_user`¶
Parameters:
* **session_id** - session ID returned by `login`
* **username** (_string_) - username
Delete a user.
`list_users`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about the account's shell users. The method returns an array
of structs with the following key-value pairs:
_username_
username
_machine_
the user's server (for example, `Web100`)
_shell_
the user's configured command line interpreter (for example, `bash` or `tcsh`)
_groups_
extra permissions groups of which the user is a member
## Servers¶
`list_ips`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about all of the account's machines and their IP addresses.
This method returns an array of structs with the following key-value pairs:
_machine_
machine name (for example, `Web100`)
_ip_
IP address
_is_main_
a boolean value indicating whether the IP address is the primary address for the server (true) or an extra IP address provisioned to the account (false)
`list_machines`¶
Parameters:
**session_id** - session ID returned by `login`
Get information about the account's machines. This method returns an array of
structs with the following key-value pairs:
_name_
machine name (for example, `Web100`)
_operating_system_
the machine's operating system (for example, `Centos6-64bit`)
_location_
the machine's location (for example, `USA`)
## Miscellaneous¶
`run_php_script`¶
Parameters:
* **session_id** - session ID returned by `login`
* **script** (_string_) - an absolute path to script (or path to the script starting from the user's home directory)
* **code_before** (_string_) - PHP code to be executed before `script`
Run PHP code followed by a PHP script. The PHP code and script is run as the
user.
`set_apache_acl`¶
Parameters:
* **session_id** - session ID returned by `login`
* **paths** (_string or array of strings_) - path from home directory
* **permission** (_string_) - `r`, `w`, or `x` or a combination thereof (optional, default: `rwx`)
* **recursive** (_boolean_) - whether Apache's access is granted recursively (optional, default: false)
Grant the machine-wide Apache instance access to files or directories.
`system`¶
Parameters:
* **session_id** - session ID returned by `login`
* **cmd** (_string_) - a shell command to be executed
Execute a command as the user, as if through SSH. If an application was
installed previously in the session, the command will be run from the
directory where that application was installed.
Note
If `cmd` writes to standard error, the API method will return an XML-RPC
fault.
### [Table Of Contents](index.html)
* API Reference
* General
* Email
* Mailboxes
* Addresses
* Websites and Domains
* Applications
* Cron
* DNS
* Databases
* Files
* Shell Users
* Servers
* Miscellaneous
#### Previous topic
[ Tutorial ](tutorial.html)
#### Next topic
[ Application Types ](apps.html)
### Search the documentation
Example: "configure email" or "create database"
#### PDF download
[Download this document as PDF](http://docs.webfaction.com/xmlrpc-
api/WebFactionAPIDocumentation.pdf)
### Feedback
Send us your feedback on this documentation.
![](_static/ajax-loader.gif)
### Navigation
* [index](genindex.html)
* [next](apps.html)
* [previous](tutorial.html)
* [WebFaction API Documentation](index.html)
[Features & prices](https://www.webfaction.com/features) [Sign
up](https://www.webfaction.com/signup) [Contact
us](http://www.webfaction.com/contact) [Affiliate
program](http://www.webfaction.com/services/affiliate)
[Support](https://help.webfaction.com/)
[Legal](https://www.webfaction.com/tos)
[Jobs](http://www.webfaction.com/jobs) [Blog](http://blog.webfaction.com)
[Control panel login](https://my.webfaction.com)
(C) 2009-2014 Swarma Limited. WebFaction is a service of Swarma Limited.
Registered in England and Wales 5729350 - VAT registration number 877397162
"""
CLI tools for webfaction API. The project seem abandoned already
so I've just fork it to my github account and start adding few
unimplemented command.
https://pypi.python.org/pypi/webf
https://github.com/k4ml/webf
After playing around with the API for a while, I realized
that we don't really need fancy library to access the API.
It pretty straightforward, the API only accept either plain
string, boolean or list as parameters and return either a dict
or list of dicts. So here a client to the whole APIs in just
106 lines of code:
Ping me @k4ml (twitter/github) if you need any help.
TODO:-
* Pull list of commands from http://docs.webfaction.com/xmlrpc-api/apiref.html
for easy reference.
"""
import re
import sys
import xmlrpclib
import logging
import os.path
import ConfigParser
API_URL = 'https://api.webfaction.com/'
CONF = os.path.expanduser('~/.webfrc')
class WebFactionXmlRpc(object):
'''WebFaction XML-RPC server proxy class'''
def __init__(self, login=True):
self.log = logging.getLogger('webf')
self.log.addHandler(logging.StreamHandler())
self.session_id = None
self.server = None
if login:
self.login()
def get_config(self):
'''Get configuration file from user's directory'''
if not os.path.exists(CONF):
self.log.error("Set your username/password in %s" % CONF)
self.log.error("The format is:")
self.log.error(" [main]")
self.log.error(" username=<username>")
self.log.error(" password=<password>")
sys.exit(1)
config = ConfigParser.ConfigParser()
config.readfp(open(CONF))
username = config.get('main', 'username')
password = config.get('main', 'password')
return (username, password)
def login(self):
'''Login to WebFaction and get a session_id'''
try:
http_proxy = os.environ['http_proxy']
except KeyError:
http_proxy = None
username, password = self.get_config()
self.server = xmlrpclib.Server(API_URL, transport=http_proxy)
self.session_id, account = self.server.login(username, password)
self.log.debug("self.session_id %s account %s" % (self.session_id,
account))
def _show(self, result, id_field='name', pattern=None):
if isinstance(result, dict):
print result
return
for item in result:
if pattern and pattern != 'None':
to_match = item[id_field] + ' ' + item.get('description', '')
if not re.search(pattern, to_match):
continue
for key, value in item.items():
if key == id_field:
print value
else:
print "\t", "%s: " % key, value
def _call(self, command, *args):
'''Generic command'''
func = getattr(self.server, command)
try:
result = func(self.session_id, *args)
self.log.debug(result)
except xmlrpclib.Fault, errmsg:
self.log.error(errmsg)
return 1
return result
def main():
if len(sys.argv) < 2:
print 'Need command'
print 'Example: python webf.py list_ips'
print 'Example: python webf.py create_website web1 "216.185.102.19" true "web1.biz,store1" "store1:/,django:/admin"'
sys.exit(1)
command = sys.argv[1]
args = sys.argv[2:]
new_args = []
for argument in args:
if argument in ('true', 'false'):
argument = True if argument == 'true' else False
new_args.append(argument)
continue
if ':' in argument:
mapping_list = argument.split(',')
argument = [(key, val) for key, val in [mapping.split(':') for mapping in mapping_list]]
new_args += argument
continue
if ',' in argument:
argument = argument.split(',')
new_args.append(argument)
continue
new_args.append(argument)
print command, new_args
wf = WebFactionXmlRpc()
result = wf._call(command, *new_args)
wf._show(result)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment