Header

  1. View current page

    하늘모자의 노트

Profile_img_60x60_01
2

스프링노트 Python 예제

 

스프링노트 Python 예제

 

스노피 소스 읽고 쓰기의 내용과 같은 페이지의 루비스크립트 소스 샘플을 참고했습니다.

 

사용법

 

페이지의 내용을 가져올 때:

  1. python test_springnote.py get yyy

(yyy는 페이지번호입니다)

 

페이지의 내용을 올릴 때:

  1. python test_springnote.py put yyy

(yyy는 페이지번호입니다)

내용을 입력후에 Ctrl-Z (Linux의 경우에는 Ctrl-D)

 

springnote.conf

설정파일입니다.

  1. [springnote]
  2. open_id: open ID
  3. user_key: user key
  4. app_id: application ID
  5. app_key: application key

 

test_springnote.py

 springnote.py

 

  1. import sys

  2. import random

  3. import datetime

  4. from ConfigParser import SafeConfigParser

  5. import sha

  6. import base64

  7. import urllib

  8. import httplib

  9. import cgi

  10. from xml.etree.ElementTree import XML

     

  11. class TestSpringNote:

  12.     def __init__(self):

  13.         self.config = SafeConfigParser()

  14.         self.config.read("springnote.conf")

  15.         open_id = self.config.get('springnote', 'open_id')

  16.         user_key = self.config.get('springnote', 'user_key')

  17.         app_id = self.config.get('springnote', 'app_id')

  18.         app_key = self.config.get('springnote', 'app_key')

  19.         nonce = "%016x" % random.randint(0, 16 ** 16 - 1)

  20.         created = datetime.datetime.utcnow().isoformat()[:19] + 'Z'

  21.         digested_key = sha.new(nonce + created + user_key + app_key).hexdigest()

     

  22.         username = urllib.quote(open_id)

  23.         password = urllib.quote('%s,%s,%s,%s' % (app_id, nonce, created, digested_key))

  24.         self.basic_auth = 'Basic %s' % base64.b64encode('%s:%s' % (username, password))

     

  25.     def httpReq(self, method, uri, data = None):

  26.         con = httplib.HTTPConnection('api.springnote.com:80')

  27.         con.putrequest(method, uri)

  28.         con.putheader('Authorization', self.basic_auth)

  29.         if data: con.putheader('Content-Length', len(data))

  30.         con.endheaders()

  31.         if data: con.send(data)

  32.         res = con.getresponse()

  33.         r = res.read()

  34.         con.close()

  35.         print XML(r).findtext('source')

  36.  

  37.     def getPage(self, page_id):

  38.         self.httpReq('GET', '/pages/%s.xml' % page_id)

     

  39.     def putPage(self, page_id):

  40.         print 'input page content:'

  41.         src = sys.stdin.read()

  42.         src_html ='<p>%s</p>' % cgi.escape(src).replace('\r', '').replace('\n', '</p>\n<p>')

  43.         data = '<page><source>%s</source></page>' % cgi.escape(src_html)

  44.         self.httpReq('PUT', '/pages/%s.xml' % page_id, data)

     

  45.     def main(self, cmd, arg):

  46.         if cmd == 'get':

  47.             self.getPage(arg)

  48.         elif cmd == 'put':

  49.             self.putPage(arg)

  50.  

    if __name__ == "__main__":

  51.     TestSpringNote().main(sys.argv[1], sys.argv[2])

 

 

Tags

History

Last edited on 06/22/2007 13:45 by 하늘모자

Comments (0)

You must log in to leave a comment. Please sign in.