Linux下不支持QQ等功能豐富的IM,雖然可以通過(guò)wine運(yùn)行QQ2012,但是還是喜歡在gtalk群中聊天,gtalk群不支持圖片方式,這就要靠我們大家自己來(lái)解決了,eleven開(kāi)放了一個(gè)Image上傳和顯示接口,提供了使用curl來(lái)解決,但是我們公司的網(wǎng)絡(luò)使用squid禁止了curl的訪問(wèn),所以整天看他們這么爽的分享圖片我也不甘心阿,所以就使用Python寫(xiě)了一個(gè)分享圖片的腳本
實(shí)現(xiàn)
使用scrot截圖,然后使用urllib2庫(kù)上傳圖片,如果存在PyQt4庫(kù)則會(huì)將結(jié)果放到剪貼板上,如果不存在則輸出,自行復(fù)制
代碼
#!/usr/bin/env python # -*- coding:utf-8 -*- # # Author : cold # E-mail : wh_linux@126.com # Date : 13/01/21 09:54:39 # Desc : 貼代碼和圖片 # import urllib2, json import mimetools import mimetypes import itertools __host__ = "http://eleveni386.7axu.com" class Form(object): def __init__(self): self.form_fields = [] self.files = [] self.boundary = mimetools.choose_boundary() self.content_type = "application/x-www-form-urlencoded" return def get_content_type(self): return self.content_type def add_field(self, name, value): self.form_fields.append((name, value)) return def add_file(self, fieldname, filename, fileHandle, mimetype=None): body = fileHandle.read() if mimetype is None: mimetype = ( mimetypes.guess_type(filename)[0] or 'applicatioin/octet-stream') self.files.append((fieldname, filename, mimetype, body)) self.content_type = 'multipart/form-data; boundary=%s' % self.boundary return def __str__(self): parts = [] part_boundary = '--' + self.boundary parts.extend( [ part_boundary, 'Content-Disposition: form-data; name="%s"' % name, '', value, ] for name, value in self.form_fields) if self.files: parts.extend([ part_boundary, 'Content-Disposition: form-data; name="%s"; filename="%s"' % (field_name, filename), 'Content-Type: %s' % content_type, '', body, ] for field_name, filename, content_type, body in self.files) flattened = list(itertools.chain(*parts)) flattened.append('--' + self.boundary + '--') flattened.append('') return ' '.join(flattened) class HttpHelper(object): def __init__(self, url = None, form = None, method = 'GET'): self._url = url self._form = form self._body = str(form) self._method = method self._dst_url = None if url: self.make_request() def make_request(self): url = self._url if not self._url.startswith('http://'): url = 'http://' + self._url self.request = urllib2.Request(url) if self._form: self.add_header("Content-Type", self._form.get_content_type()) self.add_header("Content-Length", len(self._body)) self.request.add_data(self._body) def add_header(self, key, val): self.request.add_header(key, val) def change(self, url, params = {}, method = 'GET'): self._url = url self._params = params self._method = method self.make_request() def open(self): response = urllib2.urlopen(self.request) content = response.read() self._dst_url = response.geturl() try: return json.loads(content) except: return content if __name__ == "__main__": import argparse import os parser = argparse.ArgumentParser() parser.add_argument(dest="path", nargs="?") args = parser.parse_args() if args.path: path = args.path else: path = r"/tmp/tmpscrot.png" os.system("scrot -s {0}".format(path)) form = Form() filename = os.path.split(path)[-1] form.add_file(fieldname='mypic', filename=filename, fileHandle=open(path)) http = HttpHelper( __host__ + '/Image/', form) url = http.open() try: from PyQt4.QtGui import QApplication app = QApplication([]) cb = QApplication.clipboard() cb.setText(url) except: print url
安裝
將上面代碼保存一個(gè)文件,放在PATH路徑里,賦予執(zhí)行權(quán)限即可
使用
默認(rèn)的不跟圖片地址則會(huì)截圖,截圖完畢后自動(dòng)分享,如安裝了PyQt4庫(kù)則會(huì)將結(jié)果放到剪貼板,如沒(méi)有則輸出結(jié)果.如果腳本給了圖片路徑參數(shù)則上傳給定路徑的圖片
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com