当前位置: 首页>>代码示例>>Python>>正文


Python StringIO.read方法代码示例

本文整理汇总了Python中fs.filelike.StringIO.read方法的典型用法代码示例。如果您正苦于以下问题:Python StringIO.read方法的具体用法?Python StringIO.read怎么用?Python StringIO.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在fs.filelike.StringIO的用法示例。


在下文中一共展示了StringIO.read方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: upload

# 需要导入模块: from fs.filelike import StringIO [as 别名]
# 或者: from fs.filelike.StringIO import read [as 别名]
 def upload(self, path, f):
     import pdb; pdb.set_trace()
     if isinstance(f, basestring):
         # upload given string as file's contents.
         f = StringIO(f)
     l = None
     try:
         l = len(f)
     except:
         try:
             l = os.fstat(f.fileno()).st_size
         except:
             try:
                 f.seek(0, 2)
                 l = f.tell()
                 f.seek(0)
             except:
                 raise Exception('Could not determine length of file!')
     dirname, basename = pathsplit(path)
     try:
         info = self.info(path)
     except:
         try:
             info = self.info(dirname)
         except:
             raise Exception('Cannot upload to non-existent directory!')
     url = '%s/%s/%s' % (ULURL, self.auth_token, info['inode'])
     host = urlparse.urlparse(url).hostname
     conn = httplib.HTTPConnection(host, 443)
     boundary = mimetools.choose_boundary()
     fields = {
         'boundary': boundary,
         'mime': mimetypes.guess_type(basename)[0] or 'application/octet-stream',
         'name': basename,
     }
     head = MULTIPART_HEAD % fields
     tail = MULTIPART_TAIL % fields
     l += len(head) + len(tail)
     headers = {
         'Content-Length': l,
         'Content-Type': 'multipart/form-data; boundary=%s' % boundary,
     }
     conn.request('POST', url, '', headers)
     # now stream the file to box.net.
     conn.send(head)
     while True:
         data = f.read(4096)
         if not data:
             break
         conn.send(data)
     conn.send(tail)
     r = conn.getresponse()
     if r.status != 200:
         raise Exception('Error uploading data!')
开发者ID:Gianfranco753,项目名称:fs-boxnet,代码行数:56,代码来源:boxnetfs.py


注:本文中的fs.filelike.StringIO.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。