2016-05-19 06:51:20 -05:00
<!DOCTYPE html>
2016-04-11 09:45:35 -05:00
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" / >
2016-04-18 09:34:43 -05:00
< title > Interact iRedAdmin-Pro RESTful API with Python< / title >
2016-04-11 09:45:35 -05:00
< link rel = "stylesheet" type = "text/css" href = "./css/markdown.css" / >
< / head >
< body >
< div id = "navigation" >
2016-04-19 12:48:51 -05:00
< a href = "/index.html" target = "_blank" >
< img alt = "iRedMail web site"
src="./images/logo-iredmail.png"
style="vertical-align: middle; height: 30px;"
/>
< span > iRedMail< / span >
< / a >
2016-04-18 09:34:43 -05:00
// < a href = "./index.html" > Document Index< / a > < / div > < h1 id = "interact-iredadmin-pro-restful-api-with-python" > Interact iRedAdmin-Pro RESTful API with Python< / h1 >
2016-04-11 10:07:03 -05:00
< div class = "admonition note" >
< p class = "admonition-title" > Note< / p >
2016-04-11 09:45:35 -05:00
< ul >
2016-04-18 09:34:43 -05:00
< li > For more details about iRedAdmin-Pro RESTful API, please read document:
< a href = "./iredadmin-pro.restful.api.html" > iRedAdmin-Pro: RESTful API< / a > .< / li >
< li > If you need an API which has not yet been implemented, don't hesitate to
< a href = "../contact.html" > contact us< / a > .< / li >
2016-10-24 18:17:24 -05:00
< li > Our sample code below requires third-party Python module < code > requests< / code > . For
2016-04-18 09:34:43 -05:00
more details, please read its official documentation:
< a href = "http://docs.python-requests.org/en/master/" > Requests: HTTP for Humans< / a > < / li >
2016-04-11 09:45:35 -05:00
< / ul >
< / div >
2016-04-18 09:34:43 -05:00
< p > Sample Python code to interact iRedAdmin-Pro RESTful API.< / p >
2016-04-11 09:45:35 -05:00
< pre > < code > import sys
import requests
2016-04-18 09:34:43 -05:00
url = 'http://< server> /iredadmin/api'
2016-04-11 09:45:35 -05:00
2016-04-18 09:34:43 -05:00
# Admin email address and password.
admin = 'postmaster@mydomain.com'
2016-04-11 09:45:35 -05:00
pw = 'my_password'
# Login
2016-04-18 09:34:43 -05:00
r = requests.post(url + '/login', data={'username': admin,
'password': pw})
2016-04-11 09:45:35 -05:00
2016-04-19 12:48:51 -05:00
# Get returned JSON data
2016-04-11 09:45:35 -05:00
data = r.json()
2016-04-19 12:48:51 -05:00
if not data['success']:
2016-04-11 09:45:35 -05:00
sys.exit('Login failed')
cookies = r.cookies
2016-04-18 09:34:43 -05:00
# Create domain: test.com
requests.post(url + '/domain/test.com',
2016-04-11 09:45:35 -05:00
cookies=cookies,
data={'defaultQuota': '1024'})
2016-04-18 09:34:43 -05:00
# Create user: zhb@test.com
requests.post(url + '/user/zhb@test.com',
2016-04-11 09:45:35 -05:00
cookies=cookies,
2016-04-18 09:34:43 -05:00
data={'cn': 'My Name',
'password': '1@Password',
'preferredLanguage': 'zh_CN',
2016-04-11 09:45:35 -05:00
'mailQuota': 2048})
2016-04-18 09:34:43 -05:00
# Create list: list@test.com. Note: OpenLDAP only.
requests.post(url + '/maillist/list@test.com',
cookies=cookies,
data={'cn': 'My List'})
2016-04-12 10:37:25 -05:00
2016-04-18 09:34:43 -05:00
# Create alias: alias@test.com
requests.post(url + '/alias/alias@test.com',
2016-04-11 22:29:47 -05:00
cookies=cookies,
data={'cn': 'My Alias'})
2016-04-18 09:34:43 -05:00
# Update user: zhb@test.com
requests.put(url + '/user/zhb@test.com',
cookies=cookies,
data={'cn': 'My New Name',
'password': 'WHDZ2@5yxORvVqzYY',
'transport': 'dovecot',
'language': 'en_US',
'quota': 2048})
2016-04-11 22:29:47 -05:00
2016-04-18 09:34:43 -05:00
# Delete user: zhb@test.com
requests.delete(url + '/user/zhb@test.com', cookies=cookies)
2016-04-11 22:29:47 -05:00
2016-04-18 09:34:43 -05:00
# Delete mailing list: list@test.com. Note: OpenLDAP only.
requests.delete(url + '/maillist/list@test.com', cookies=cookies)
# Delete alias: alias@test.com
requests.delete(url + '/alias/alias@test.com', cookies=cookies)
# Delete domain: test.com
requests.delete(url + '/domain/test.com', cookies=cookies)
2016-04-11 22:29:47 -05:00
< / code > < / pre >
2016-04-11 09:45:35 -05:00
< h2 id = "see-also" > See Also< / h2 >
< ul >
2016-04-18 09:34:43 -05:00
< li > < a href = "./iredadmin-pro.restful.api.curl.html" > Interact iRedAdmin-Pro RESTful API with < code > curl< / code > < / a > < / li >
2016-05-19 06:51:20 -05:00
< / ul > < div class = "footer" >
< p style = "text-align: center; color: grey;" > All documents are available in < a href = "https://bitbucket.org/zhb/iredmail-docs/src" > BitBucket repository< / a > , and published under < a href = "http://creativecommons.org/licenses/by-nd/3.0/us/" target = "_blank" > Creative Commons< / a > license. You can < a href = "https://bitbucket.org/zhb/iredmail-docs/get/tip.tar.bz2" > download the latest version< / a > for offline reading. If you found something wrong, please do < a href = "http://www.iredmail.org/contact.html" > contact us< / a > to fix it.< / p >
< / div >
< script type = "text/javascript" >
2016-04-11 09:45:35 -05:00
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-3293801-21', 'auto');
ga('send', 'pageview');
< / script >
< / body > < / html >