Skicka filer skript
Jag tänkte dela med mig av mitt skicka filer skript. Det är ett skript som öppnar en superenkel tillfällig webbserver för endast en fil en gång och placerar url:en i clipboard:et.
Det är skrivet i python och kräver win32all (för clipboard grejen).
http://www.python.org/
http://starship.python.net/~skippy/win32/Downloads.html
Detta är skriptet
import socket
import os
import sys
import urllib
import win32clipboard
file_path = sys.argv[1]
# bind and listen
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
s.listen(1)
# set the clipboard to the url
ip = urllib.urlopen("http://whatismyip.com/automation/n09230945.asp").read()
url = "http://"+ip+":"+str(s.getsockname()[1])
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(url)
win32clipboard.CloseClipboard()
print url
conn, addr = s.accept()
print 'Connected by', addr
# get file length
f = file(file_path, "rb")
f.seek(0, os.SEEK_END)
file_len = f.tell()
f.seek(0, os.SEEK_SET)
# get filename
file_name = file_path.split(os.sep)[-1]
# send http headers
conn.sendall("HTTP/1.1 200 OK\r\n")
conn.sendall("Content-Disposition: attachment; filename="+file_name+"\r\n")
conn.sendall("Content-Type: application/force-download\r\n")
conn.sendall("Content-Description: File Transfer\r\n")
conn.sendall("Content-Length: "+str(file_len)+"\r\n\r\n")
# send the file
while True:
data = f.read(4096)
if len(data) == 0:
break
conn.sendall(data)
os.system("pause")
# cleanup
f.close()
s.close()
conn.close()
Själv har jag lagt det i "shell:sendto" vilket gör det smidigt att bara högerklicka och trycka skicka till.
Kommentarer? Det finns en skum bugg. Om man tar bort system("pause") i slutet slutar skriptet att fungera bra. Någon som kan förstå varför?