Skip to content

Commit 500c8cc

Browse files
committed
Improve XML-RPC example scripts and configuration
1 parent e97b9d1 commit 500c8cc

File tree

1 file changed

+76
-51
lines changed

1 file changed

+76
-51
lines changed

content/admin/configuration/listen.md

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -458,61 +458,85 @@ Handles XML-RPC requests to execute
458458
It is configured as a request handler in
459459
[ejabberd_http](/admin/configuration/listen/#ejabberd-http).
460460

461+
This is the minimum configuration required to enable the feature:
462+
463+
```yaml
464+
listen:
465+
-
466+
port: 5280
467+
module: ejabberd_http
468+
request_handlers:
469+
/xmlrpc: ejabberd_xmlrpc
470+
471+
api_permissions:
472+
"public commands":
473+
who:
474+
ip: 127.0.0.1/8
475+
what:
476+
- connected_users_number
477+
```
478+
479+
Example Python3 script:
480+
481+
```python
482+
import xmlrpc.client
483+
server = xmlrpc.client.ServerProxy("http://127.0.0.1:5280/xmlrpc/");
484+
print(server.connected_users_number())
485+
```
486+
461487
By default there is no restriction to who can execute what commands,
462488
so it is strongly recommended that you configure restrictions using
463489
[API Permissions](/developer/ejabberd-api/permissions/).
464490

465-
This is the minimum configuration required to enable the feature:
491+
This example configuration adds some restrictions (only requests from localhost are accepted, the XML-RPC query must include authentication credentials of a specific account registered in ejabberd, and only two commands are accepted):
492+
493+
```yaml
494+
listen:
495+
-
496+
port: 5280
497+
ip: "::"
498+
module: ejabberd_http
499+
request_handlers:
500+
/xmlrpc: ejabberd_xmlrpc
501+
502+
api_permissions:
503+
"some XMLRPC commands":
504+
from: ejabberd_xmlrpc
505+
who:
506+
- ip: 127.0.0.1
507+
- user: user1@localhost
508+
what:
509+
- registered_users
510+
- connected_users_number
511+
```
512+
513+
Example Python3 script for that restricted configuration:
514+
515+
```python
516+
import xmlrpc.client
517+
server = xmlrpc.client.ServerProxy("http://127.0.0.1:5280/xmlrpc/");
518+
519+
params = {}
520+
params['host'] = 'localhost'
521+
522+
auth = {'user': 'user1',
523+
'server': 'localhost',
524+
'password': 'mypass11',
525+
'admin': True}
526+
527+
def calling(command, data):
528+
fn = getattr(server, command)
529+
return fn(auth, data)
530+
531+
print(calling('registered_users', params))
532+
```
533+
534+
Please notice, when using the old Python2, replace the two first lines with:
466535

467-
listen:
468-
-
469-
port: 4560
470-
module: ejabberd_http
471-
request_handlers:
472-
/: ejabberd_xmlrpc
473-
474-
Example Python script:
475-
476-
import xmlrpclib
477-
server = xmlrpclib.Server('http://127.0.0.1:4560/');
478-
params = {}
479-
params["host"] = "localhost"
480-
print server.registered_users(params)
481-
482-
This example configuration adds some restrictions:
483-
484-
listen:
485-
-
486-
port: 5281
487-
ip: "::"
488-
module: ejabberd_http
489-
request_handlers:
490-
/api: mod_http_api
491-
/xmlrpc: ejabberd_xmlrpc
492-
493-
api_permissions:
494-
"some XMLRPC commands":
495-
from: ejabberd_xmlrpc
496-
who:
497-
- ip: 127.0.0.1
498-
- user: user1@localhost
499-
what:
500-
- registered_users
501-
- connected_users_number
502-
503-
With that configuration, it is possible to execute two specific commands using
504-
`ejabberd_xmlrpc`, with two access restrictions. Example Python script:
505-
506-
import xmlrpclib
507-
server = xmlrpclib.Server('http://127.0.0.1:5281/xmlrpc')
508-
LOGIN = {'user':'user1',
509-
'server':'localhost',
510-
'password':'mypass11',
511-
'admin':True}
512-
def calling(command, data):
513-
fn = getattr(server, command)
514-
return fn(LOGIN, data)
515-
print calling('registered_users', {'host':'localhost'})
536+
```python
537+
import xmlrpclib
538+
server = xmlrpclib.Server("http://127.0.0.1:5280/xmlrpc/");
539+
```
516540

517541
It's possible to use OAuth for authentication instead of plain password, see
518542
[OAuth Support](/developer/ejabberd-api/oauth/).
@@ -522,7 +546,8 @@ listener, see the old document for reference and example configuration:
522546
[Listening Module](/admin/configuration/old/#listening-module).
523547

524548
Just for reference, there's also the old
525-
[`ejabberd_xmlrpc documentation`](https://ejabberd.im/ejabberd_xmlrpc).
549+
[`ejabberd_xmlrpc documentation`](https://ejabberd.im/ejabberd_xmlrpc)
550+
with example clients in other languages.
526551

527552
# Examples
528553

0 commit comments

Comments
 (0)