python使用新浪微博api上传图片到微博示例
时间:2021-10-13 02:15:06
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_()
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python判断某个用户对某个文件的权限
Python字符串和其常用函数合集
一篇文章带你入门python之推导式
浅谈Golang内存逃逸
asp 实现的冒泡排序程序
构建成功web应用的十项黄金法则
![](https://img.aspxhome.com/file/UploadPic/20109/17/01-99s.jpg)
ASP.NET Core Authentication认证实现方法
![](https://img.aspxhome.com/file/2023/8/78078_0s.jpg)
Ajax+Servlet+jsp显示搜索效果
![](https://img.aspxhome.com/file/2023/4/65744_0s.jpg)
在MySQL数据库中如何修改密码及访问限制
python库pydantic的简易入门教程
ASP 支持中文的len(),left(),right()的函数代码
HTML5中 b 和 i 标签将语义化
asp.net C#实现解压缩文件的方法
JS清空上传控件input(type="file")的值的代码第1/2页
golang实现的文件上传下载小工具
JS获取checkbox的个数简单实例
Http头 Range、Content-Range
Mootools 1.2教程(19)——Tooltips
Windows安装Anaconda并且配置国内镜像的详细教程
![](https://img.aspxhome.com/file/2023/8/61708_0s.png)
Python实现超快窗口截图功能详解
![](https://img.aspxhome.com/file/2023/5/63945_0s.png)