스노피 소스 읽고 쓰기[Python]
- 참고한 소스
- 하늘모자님이 작성하신 python에서 스노피 소스 읽고 쓰기 http://doc.springnote.com/pages/313680 를 참고하여 8월 1일부터 바뀐 API 인증에 맞게 하였습니다.
- ias 님이 루비로 작성하신 스노피 소스 읽고 쓰기 http://dev.springnote.com/pages/3364 를 참고하여 새로 바뀐 인증방식에 적용 하였습니다.
- 변화된 사항
- 하늘모자님의 소스에서 바뀐 부분은 HTTPS 을 이용한 인증부분과 쉘상에서 입력받을때 \n 을 무조건 없애던것을 수정하였으며, 탭을 공백 4개로 표시하게 하였습니다.
- ias 님의 소스에서와 같이 파일을 read 하여 data 를 작성하는것은 src=sys.stdin.read() 부분만 src = open('파일이름').read() 로 해주시면 됩니다.
- 특이사항
- from xml.etree.ElementTree import XML 부분의 경우 python 2.5 이상에서만 됩니다. 굳이 필요하지 않으므로 해당 부분을 제거하고 소스에서 print XML(r).findtext('source') 부분만 그냥 print r 로 해도 되며 다른 xml 모듈을 사용해서 가공하시면 됩니다.
- multipart/form-data 를 통한 파일 첨부는 제 능력 부족으로 구현하지 못하고 있습니다. 정보 있으시면 연락주세요
- 아직 잘 작성하지 못해서 좋은 정보나 관련된 정보 주시면 감사하게 받겠습니다. 연락은 제 블로그나 메일로 주세요
-
연락처
- email : nakada@한메일
- 블로그 : http://seapy.com
- #!/usr/bin/python
- import sys
- from ConfigParser import SafeConfigParser
- import base64
- import urllib
- import httplib
- import cgi
- from xml.etree.ElementTree import XML
- class SpringNote:
- def __init__(self):
- self.config = SafeConfigParser()
- self.config.read('spring.conf')
- open_id = self.config.get('springnote','open_id')
- user_key = self.config.get('springnote','user_key')
- app_key = self.config.get('springnote','app_key')
- username = urllib.quote(open_id)
- passwd = urllib.quote('%s.%s' % (user_key, app_key))
- self.basic_auth = 'Basic %s' % base64.b64encode('%s:%s' % (username, passwd))
- def httpReq(self, method, uri, data = None):
- HOSTNAME = 'api.springnote.com'
- conn = httplib.HTTPSConnection(HOSTNAME)
- conn.putrequest(method, uri)
- conn.putheader('Authorization', self.basic_auth)
- if data :
- conn.putheader('Content-Type','application/xml')
- conn.putheader('Content-Length', len(data))
- conn.endheaders()
- if data :
- conn.send(data)
- response = conn.getresponse()
- r = response.read()
- conn.close()
- print XML(r).findtext('source')
- def getPage(self, page_id):
- self.httpReq('GET','/pages/%s.xml' % page_id)
- def putPage(self, page_id):
- print 'input page content:'
- src = sys.stdin.read()
- src_html = '<p>%s</p>' % cgi.escape(src).replace('\r', '').replace('\t',' '*4).replace('\n', '</p>\n<p>').replace('<p></p>','<p> </p>')
- data = "<page><source>%s</source></page>" % cgi.escape(src_html)
- self.httpReq('PUT', '/pages/%s.xml' % page_id, data)
- def main(self, cmd, arg):
- if cmd == 'get':
- self.getPage(arg)
- elif cmd == 'put':
- self.putPage(arg)
- if __name__ == "__main__":
- SpringNote().main(sys.argv[1], sys.argv[2])
History
Last edited on 10/14/2007 21:20 by nakada
Comments (0)