Skip to content

Instantly share code, notes, and snippets.

@eight
Last active Jan 13, 2016
Embed
What would you like to do?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""yurpc2.py
XML-RPCで郵便番号から住所を取得するPythonスクリプト
"""
import sys
import string
try:
from xmlrpc.client import ServerProxy
except ImportError:
from xmlrpclib import ServerProxy
tmpl = {}
#住所用のテンプレート
tmpl['p'] = string.Template("""<div id="p$postcode" class="addr_p">
<span class="postcode">$postcode</span>
<span class="jiscode">$jiscode</span>
<span class="pref">$pref</span>
<span class="city">$city</span>
<span class="town">$town</span>
<span class="pref_kana">$pref_kana</span>
<span class="city_kana">$city_kana</span>
<span class="town_kana">$town_kana</span>
</div>
""")
#事業所用のテンプレート
tmpl['j'] = string.Template("""<div id="p$postcode" class="addr_j">
<span class="postcode">$postcode</span>
<span class="jiscode">$jiscode</span>
<span class="addr_name">$addr_name</span>
<span class="addr_name_kana">$addr_name_kana</span>
<span class="pref">$pref</span>
<span class="city">$city</span>
<span class="town">$town</span>
<span class="other">$other</span>
</div>
""")
def fetch_addr(postcode):
server = ServerProxy("http://yubin.senmon.net/service/xmlrpc/")
try:
r = server.yubin.fetchAddressByPostcode(postcode)
for addr in r:
return tmpl[addr['data_type']].substitute(addr)
except xmlrpclib.Fault as fault:
return "Fault [#%s %s] postcode = %s" % (fault.faultCode, fault.faultString, postcode)
def run_test():
for p in ('1378088', '1140011'):
print(fetch_addr(p))
if __name__=='__main__':
if len(sys.argv)>1:
print(fetch_addr(sys.argv[1]))
else:
run_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment