Header

  1. View current page

    나까다의 뇌 속

Profile_img_60x60_01
2

스노피 소스 읽고 쓰기[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 를 통한 파일 첨부는 제 능력 부족으로 구현하지 못하고 있습니다. 정보 있으시면 연락주세요
    • 아직 잘 작성하지 못해서 좋은 정보나 관련된 정보 주시면 감사하게 받겠습니다. 연락은 제 블로그나 메일로 주세요
  • 연락처 

 

  1. #!/usr/bin/python
  2.  
  3. import sys
  4. from ConfigParser import SafeConfigParser
  5. import base64
  6. import urllib
  7. import httplib
  8. import cgi
  9. from xml.etree.ElementTree import XML
  10.  
  11. class SpringNote:
  12.     def __init__(self):
  13.         self.config = SafeConfigParser()
  14.         self.config.read('spring.conf')
  15.         open_id = self.config.get('springnote','open_id')
  16.         user_key = self.config.get('springnote','user_key')
  17.         app_key = self.config.get('springnote','app_key')
  18.  
  19.         username = urllib.quote(open_id)
  20.         passwd = urllib.quote('%s.%s' % (user_key, app_key))
  21.         self.basic_auth = 'Basic %s' % base64.b64encode('%s:%s' % (username, passwd))
  22.  
  23.     def httpReq(self, method, uri, data = None):
  24.         HOSTNAME = 'api.springnote.com'
  25.         conn = httplib.HTTPSConnection(HOSTNAME)
  26.         conn.putrequest(method, uri)
  27.         conn.putheader('Authorization', self.basic_auth)
  28.         if data :
  29.             conn.putheader('Content-Type','application/xml')
  30.             conn.putheader('Content-Length', len(data))
  31.         conn.endheaders()
  32.         if data :
  33.             conn.send(data)
  34.         response = conn.getresponse()
  35.         r = response.read()
  36.         conn.close()
  37.         print XML(r).findtext('source')
  38.  
  39.     def getPage(self, page_id):
  40.         self.httpReq('GET','/pages/%s.xml' % page_id)
  41.  
  42.     def putPage(self, page_id):
  43.         print 'input page content:'
  44.         src = sys.stdin.read()
  45.         src_html = '<p>%s</p>' % cgi.escape(src).replace('\r', '').replace('\t','&nbsp;'*4).replace('\n', '</p>\n<p>').replace('<p></p>','<p>&nbsp;</p>')
  46.         data = "<page><source>%s</source></page>" % cgi.escape(src_html)
  47.         self.httpReq('PUT', '/pages/%s.xml' % page_id, data)
  48.  
  49.     def main(self, cmd, arg):
  50.         if cmd == 'get':
  51.             self.getPage(arg)
  52.         elif cmd == 'put':
  53.             self.putPage(arg)
  54.  
  55. if __name__ == "__main__":
  56.     SpringNote().main(sys.argv[1], sys.argv[2])

 

History

Last edited on 10/14/2007 21:20 by nakada

Comments (0)

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