{"id":2786,"date":"2016-02-26T15:39:37","date_gmt":"2016-02-26T07:39:37","guid":{"rendered":"https:\/\/drho.club\/?p=419"},"modified":"2016-02-26T15:39:37","modified_gmt":"2016-02-26T07:39:37","slug":"django-kvm-vm","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2016\/02\/26\/django-kvm-vm\/","title":{"rendered":"\u5728 Django\u4e2d\u64cd\u4f5cKVM\u4e0a\u7684\u865b\u64ec\u6a5f"},"content":{"rendered":"<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">Django\u662f\u5728Python\u4e2d\u975e\u5e38\u8457\u540d\u7684Web Framework\uff0c\u5982\u679c\u6211\u5011\u60f3\u8981\u900f\u904eWeb\u4ecb\u9762\u4f86\u64cd\u4f5c\u5728\u4e3b\u6a5f\u4e0a\u7684VM\uff0c\u65b9\u6cd5\u4e5f\u975e\u5e38\u7c21\u55ae\u3002\u4e3b\u8981\u7684\u7a0b\u5f0f\u8981\u5beb\u5728views.py\u4e2d\u3002\u5047\u8a2d\u6211\u5011\u8981\u8b93\u700f\u89bd\u5668\u4e00\u9032\u4e3b\u7db2\u9801\u7684\u6642\u5019\u5c31\u99ac\u4e0a\u53ef\u4ee5\u770b\u5230\u76ee\u524d\u57f7\u884c\u4e2d\u548c\u975e\u57f7\u884c\u4e2d\u7684VM\uff0c\u5247views.py\u4e2d\u8981\u9019\u6a23\u5beb\uff1a<\/p>\n<p>[code]<br \/>\nfrom django.http import HttpResponse<br \/>\nfrom django.template.loader import get_template<br \/>\nimport libvirt, sys<\/p>\n<p># Create your views here.<br \/>\ndef index(request):<br \/>\n    conn = libvirt.openReadOnly(&quot;qemu:\/\/\/system&quot;)<br \/>\n    running = []<br \/>\n    for id in conn.listDomainsID():<br \/>\n        vm = conn.lookupByID(id)<br \/>\n        running.append(vm.name())<br \/>\n    inactive = []<br \/>\n    for domname in conn.listDefinedDomains():<br \/>\n        inactive.append(domname)<br \/>\n    conn.close()<br \/>\n    template = get_template(&#8216;index.html&#8217;)<br \/>\n    html = template.render({&#8216;running&#8217;: running, &#8216;inactive&#8217;: inactive})<br \/>\n    return HttpResponse(html)<br \/>\n[\/code]<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u56e0\u70ba\u4f7f\u7528\u5230template\uff0c\u6240\u4ee5\u6211\u5011\u4e5f\u6e96\u5099\u4e86\u4e00\u500bindex.html\uff0c\u653e\u5728templates\u76ee\u9304\u4e4b\u4e0b\u3002index.html\u7684\u5167\u5bb9\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>[code]<br \/>\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n    &lt;meta charset=&#8217;utf-8&#8242;&gt;<br \/>\n    &lt;title&gt; VM Management &lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;h2&gt;Welcome to Master Monitor Management System&lt;\/h2&gt;<br \/>\n&lt;h3&gt;Running VMs&lt;\/h3&gt;<br \/>\n&lt;ul&gt;<br \/>\n    {% for dm in running %}<br \/>\n        &lt;li&gt; {{ dm }} &lt;\/li&gt;<br \/>\n    {% empty %}<br \/>\n    &lt;p&gt;\u6c92\u6709\u6b63\u5728\u57f7\u884c\u4e2d\u7684VM&lt;\/p&gt;<br \/>\n    {% endfor %}<br \/>\n&lt;\/ul&gt;<br \/>\n&lt;h3&gt;Inactive VMs&lt;\/h3&gt;<br \/>\n&lt;ul&gt;<br \/>\n    {% for dm in inactive %}<br \/>\n        &lt;li&gt; {{ dm }} &lt;\/li&gt;<br \/>\n    {% empty %}<br \/>\n    &lt;p&gt;\u6c92\u6709\u8655\u65bcinactive\u72c0\u614b\u7684VM&lt;\/p&gt;<br \/>\n    {% endfor %}<br \/>\n&lt;\/ul&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/code]<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u8981\u8b93\u7db2\u9801\u4e3b\u7db2\u5740\u53ef\u4ee5\u57f7\u884cindex()\u9019\u500b\u51fd\u6578\u53bbrender index.html\uff0curls.py\u88e1\u9762\u4e5f\u8981\u6539\u4e00\u4e0b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>[code]<br \/>\nfrom django.conf.urls import url<br \/>\nfrom django.contrib import admin<br \/>\nfrom mm.views import index<\/p>\n<p>urlpatterns = [<br \/>\n    url(r&#8217;^admin\/&#8217;, admin.site.urls),<br \/>\n    url(r&#8217;^$&#8217;, index),<br \/>\n]<br \/>\n[\/code]<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u518d\u52a0\u4e0a\u5176\u5b83\u7684\u4e00\u4e9b\u5fc5\u8981\u7684Django\u57fa\u672c\u8a2d\u5b9a\uff0c\u5c31\u5927\u529f\u544a\u6210\u4e86\u3002\u4ee5\u4e0b\u662f\u57f7\u884c\u7684\u7d50\u679c\u756b\u9762\uff1a<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\"><a href=\"https:\/\/104.es\/wp-content\/uploads\/2016\/02\/2016-02-26_15-47-01.png\" rel=\"attachment wp-att-421\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-421 aligncenter\" src=\"https:\/\/104.es\/wp-content\/uploads\/2016\/02\/2016-02-26_15-47-01.png\" alt=\"2016-02-26_15-47-01\" width=\"455\" height=\"470\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2016\/02\/2016-02-26_15-47-01.png 613w, https:\/\/104.es\/wp-content\/uploads\/2016\/02\/2016-02-26_15-47-01-291x300.png 291w, https:\/\/104.es\/wp-content\/uploads\/2016\/02\/2016-02-26_15-47-01-600x620.png 600w\" sizes=\"auto, (max-width: 455px) 100vw, 455px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django\u662f\u5728Python\u4e2d\u975e\u5e38\u8457\u540d\u7684Web Framework\uff0c\u5982\u679c\u6211\u5011\u60f3\u8981\u900f\u904eWeb\u4ecb\u9762\u4f86\u64cd\u4f5c\u5728\u4e3b\u6a5f\u4e0a\u7684VM\uff0c\u65b9\u6cd5\u4e5f\u975e\u5e38\u7c21\u55ae\u3002\u4e3b\u8981\u7684\u7a0b\u5f0f\u8981\u5beb\u5728views.py\u4e2d\u3002\u5047\u8a2d\u6211\u5011\u8981\u8b93\u700f\u89bd\u5668\u4e00\u9032\u4e3b\u7db2\u9801\u7684\u6642\u5019\u5c31\u99ac\u4e0a\u53ef\u4ee5\u770b\u5230\u76ee\u524d\u57f7\u884c\u4e2d\u548c\u975e\u57f7\u884c\u4e2d\u7684VM\uff0c\u9019\u7bc7\u6587\u7ae0\u4e2d\u63d0\u4f9b\u8a2d\u5b9a\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[97,15],"tags":[28,133,139],"class_list":["post-2786","post","type-post","status-publish","format-standard","hentry","category-kvm-libvirt","category-python","tag-django","tag-kvm","tag-libvirt"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=2786"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2786\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}