python使用新浪微博api上传图片到微博示例

yipeiwu_com4年前Python实例
import urllib.parse,os.path,time,sys
from http.client import HTTPSConnection
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

#path
ospath=sys.path[0]
if len(ospath)!=3:
    ospath+='\\'
ospath=ospath.replace('\\','/')
#api
class Api:
    def sina(self,status,pic):
        fSize=os.path.getsize(pic)
        BOUNDARY="$-img-lufei-goodboy-$"
        CRLF='\r\n'
        data=[
            #token
            '--'+BOUNDARY,
            'Content-disposition: form-data; name="access_token"',
            '',
            'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',#你的access_token
            #status
            '--'+BOUNDARY,
            'Content-disposition: form-data; name="status"',
            '',
            status,
            #pic
            '--'+BOUNDARY,
            'Content-disposition: form-data; name="pic"; filename="q_17.jpg"',
            'Content-type: image/jpeg',
            ''
        ]
        #utf-8
        data=(CRLF.join(data)+CRLF).encode('utf-8')
        closing='\r\n--'+BOUNDARY+'--\r\n'
        sumlen=len(data)+len(closing)+fSize
        #----------------------------------------
        h=HTTPSConnection('upload.api.weibo.com')
        h.putrequest('POST','/2/statuses/upload.json')
        h.putheader('Content-type','multipart/form-data; boundary=%s' % BOUNDARY)
        h.putheader('Content-length',sumlen)
        h.endheaders()
        h.send(data)
        f=open(pic,'rb')
        while True:
            data=f.read(12345)
            if not data:
                break
            h.send(data)
        f.close()
        h.send(closing.encode('utf-8'))
        r=h.getresponse()
        return r.read().decode('utf-8','ignore')
api=Api()
#ui
class Dialog(QDialog):
    def __init__(self):
        super().__init__()
        #icon,title
        self.setWindowIcon(QIcon(ospath+'weibo.ico'))
        self.setWindowTitle('weibo')
        #texteditor
        self.editor=QTextEdit()
        #textline,filebutton,submit
        self.line=QLineEdit()
        brows=QPushButton('打开')
        brows.clicked.connect(self.getFileName)
        submit=QPushButton('发表')
        submit.clicked.connect(self.submit)
        #layout
        layout=QGridLayout()
        layout.setContentsMargins(0,0,0,0)
        #addwidget
        layout.addWidget(self.editor,0,0,1,2)
        layout.addWidget(self.line,1,0,1,1)
        layout.addWidget(brows,1,1,1,1)
        layout.addWidget(submit,2,0,1,2)
        #set
        self.setLayout(layout)
    def getFileName(self):
        fileName=QFileDialog.getOpenFileName()
        self.line.setText(fileName[0])
    def submit(self):
        status=self.editor.toPlainText()
        pic=self.line.text()
        self.editor.setText(api.sina(status,pic))
app=QApplication(sys.argv)
dialog=Dialog()
dialog.show()
app.exec_()


相关文章

django 文件上传功能的相关实例代码(简单易懂)

django 文件上传功能的相关实例代码(简单易懂)

一、新建项目,在主配置文件中,修改以下内容:ALLOWED_HOSTS = ['127.0.0.1','localhost'] MED...

复制粘贴功能的Python程序

今天因为给BeauBeau提供的抽奖号码做SQL文件,一开始收到ZIP文件解开压缩之后被吓到了——29个CSV文件,每个文件保存了1000个奖券ID和号码-_-! 照上次一样,打开每个CS...

在tensorflow中设置保存checkpoint的最大数量实例

 # Set up a RunConfig to only save checkpoints&...

使用Python实现Wake On Lan远程开机功能

Wake-On-LAN简称WOL,是一种电源管理功能;如果存在网络活动,则允许设备将操作系统从待机或休眠模式中唤醒。许多主板厂商支持IBM提出的网络唤醒标准。该标准允许网络管理员远程打开P...

浅谈tensorflow中Dataset图片的批量读取及维度的操作详解

三维的读取图片(w, h, c):import tensorflow as tf   import glob import ...