python模拟上传图片_Python QQ空间模拟上传图片 代码

# -*- coding: utf-8 -*-

##模拟发送图片

from PIL import Image

import base64

import urllib

import urllib2

import datetime

print datetime.datetime.now().microsecond

import re

##第一步先上传图片,返回获得filelen和filemd5的值

def update_pic_to_qq(picpath):

f=open(picpath,'rb') #二进制方式打开图文件

ls_f=base64.b64encode(f.read()) #读取文件内容,转换为base64编码

f.close()

#print ls_f

img = Image.open(picpath)

print img.size

#exit();

hd_width=img.size[0]

hd_height=img.size[1]

url = 'http://up.qzone.com/cgi-bin/upload/cgi_upload_pic_v2'

user_agent = 'Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'

values = {'picture' : ls_f ,

'base64' : '1',

'hd_height' : hd_height,

'hd_width' : hd_width,

'hd_quality' : '96',

'output_type' : 'json',

'preupload' : '1',

'charset' : 'utf-8',

'output_charset' : 'utf-8',

'logintype' : 'sid',

'Exif_CameraMaker' : '',

'Exif_CameraModel':'',

'Exif_Time':'',

'uin':'QQ号码',

'sid' : 'QQ号码SID' }

headers = { 'User-Agent' : user_agent ,

'Referer':'http://m.qzone.com/infocenter?g_f=2425'}

data = urllib.urlencode(values)

req = urllib2.Request(url, data, headers)

response = urllib2.urlopen(req)

the_page = response.read()

#print the_page

#print '参数:',data

print '结果:',the_page

return the_page

#上传图片第二步

def update_pic_to_xiangce(filelen,md5):

url = 'http://up.qzone.com/cgi-bin/upload/cgi_upload_pic_v2'

user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 6_0_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5'

values = {'output_type' : 'json' ,

'preupload' : '2',

'md5' : md5,

'filelen' : filelen,

'batchid' : '',

'currnum' : '0',

'uploadNum' : '1',

'uploadtime' : '',

'uploadtype' : '1',

'upload_hd' : '1',

'albumtype' : '7',

'big_style':'1',

'op_src':'15001',

'charset':'utf-8',

'output_charset':'utf-8',

'uin':'QQ号码',

'sid':'QQ号码SID',

'logintype':'sid',

'mobile_dc':'actiontype%3D2%26subactiontype%3D1%26reserves%3D1%26page_type%3D2%26app_id%3D7003',

'albumid':'QQ相册ID',

'desc':'图片自定义内容',

'platformid':'52',

'platformsubid' : '11' }

headers = { 'User-Agent' : user_agent ,

'Referer':'http://m.qzone.com/infocenter?g_f=2425'}

data = urllib.urlencode(values)

req = urllib2.Request(url, data, headers)

response = urllib2.urlopen(req)

the_page = response.read()

#print the_page

#print '参数:',data

print '结果:',the_page

##运行方法

def test_main():

#p=re.compile(r'filelen" : ([0-9]+)')

#al=p.findall('"filelen" : 20319,')

#print al[0]

result=update_pic_to_qq(r'E:\tempImage\p7.jpg')

p_1=re.compile(r'filelen" : ([0-9]+)')

p_2=re.compile(r'filemd5" : "([^<>]+)"')

filelen=p_1.findall(result)

md5=p_2.findall(result)

print 'filelen',filelen

print 'md5',md5

update_pic_to_xiangce(filelen[0],md5[0])

test_main()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值