我正在尝试从Google Chrome扩展程序将图像上传到Picasa,并在构建POST时遇到一些麻烦.
这是谷歌指定用于将图像上传到Picasa的协议(链接):
Content-Type: multipart/related; boundary="END_OF_PART"
Content-Length: 423478347
MIME-version: 1.0
Media multipart posting
--END_OF_PART
Content-Type: application/atom+xml
<entry xmlns='http://www.w3.org/2005/Atom'>
<title>plz-to-love-realcat.jpg</title>
<summary>Real cat wants attention too.</summary>
<category scheme="http://schemas.google.com/g/2005#kind"
term="http://schemas.google.com/photos/2007#photo"/>
</entry>
--END_OF_PART
Content-Type: image/jpeg
...binary image data...
--END_OF_PART--
Run Code Online (Sandbox Code Playgroud)
这就是我拼凑起来试图做到这一点,从这里借用代码和扩展"clip-it-good":
function handleMenuClick(albumName, albumId, data, tab) {
chrome.pageAction.setTitle({
tabId: tab.id,
title: 'Uploading (' + data.srcUrl.substr(0, 100) + ')'
});
chrome.pageAction.show(tab.id);
var img = document.createElement('img');
img.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0); …Run Code Online (Sandbox Code Playgroud) 有一种socket方法可以获取给定网络接口的IP:
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
Run Code Online (Sandbox Code Playgroud)
返回以下内容:
>>> get_ip_address('lo')
'127.0.0.1'
>>> get_ip_address('eth0')
'38.113.228.130'
Run Code Online (Sandbox Code Playgroud)
是否有类似的方法来返回该接口的网络传输?我知道我可以阅读,/proc/net/dev但我喜欢套接字方法.