__init__.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:ShareFile 作者: umahmood 项目源码 文件源码
def run(self):
        _, ext = path.splitext(self.filename)

        try:
            mime_type = types_map[ext]
        except KeyError:
            mime_type = "application/octet-stream"

        with open(self.filepath, 'rb') as f:
            data = f.read()

        headers = {
            "Accept"            : "*/*",
            "Accept-Encoding"   : "gzip,deflate",
            "Accept-Language"   : "en-US,en;q=0.8",
            "Connection"        : "keep-alive",
            "Content-Length"    : len(data),
            "Content-Type"      : mime_type,
            "Host"              : "transfer.sh",
            "Origin"            : "https://transfer.sh",
            "Referer"           : "https://transfer.sh/",
            "User-Agent"        : "Mozilla/5.0",
            "X_FILENAME"        : self.filename
        }

        url = "https://transfer.sh/" + self.filename

        req = request.Request(url, data=data, headers=headers, method="PUT")

        try:
            show_status_message("ShareFile: uploading file...")
            with request.urlopen(req, timeout=10) as resp:
                if resp.status == 200:
                    body       = resp.read()
                    share_link = body.decode('utf-8').strip('\n')
                    msg        = template.format(share_link, "")
                else:
                    msg = template.format("Could not upload file",
                                          str(resp.status) + " " + resp.reason)
                clear_status_message()
                show_alert(msg)
        except URLError as e:
            msg = template.format(e.reason, "")
            show_alert(msg)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号