# -*- coding: cp1250 -*- # # Script: fileupload.py # Version: 0.1 # Date: 16/08/2005 # Works with python 2.4.1 or higher; ONLY on Win32 platform # # Copyright 2005 Rodion rodion7(at)o2.pl # License: http://miracle7.info/bsd-license.txt import os, httplib, mimetypes import ImageGrab import Image # Zmienne, które należy ustawić host = '127.0.0.1' # W formacie xxx.xxx.xxx.xxx np. 127.0.0.1 selector = '/uploadaction.php' # Zmienna ACTION w formularzu HTML zawsze z "/" np. "/uploadaction.php" form_file_name = 'userfile' # Zmienna pliku w formularzu HTML np. form_max_name = 'MAX_FILE_SIZE' # Opcjonalna zmienna w formularzu HTML np. form_max_value = '262144' # Wartoć opcjonalnej zmiennej MAX_FILE_SIZE w formularzu HTML storage_path = r"C:\Documents and Settings\Administrator\Moje dokumenty\Screens\\" # Gdzie zrzucać screenshoty; na końcu "\\" def printscreen(image_name): image = ImageGrab.grabclipboard() if isinstance(image, Image.Image): image.save(storage_path + image_name) return True else: return None def check_file(filename): try: size = os.path.getsize(filename) except OSError: return None if size > int(form_max_value): return None else: f = open(filename, 'rb') image_file_body = f.read() f.close() return image_file_body, True def get_content_type(filename): return mimetypes.guess_type(filename)[0] or 'application/octet-stream' def encode_multipart_formdata(fields, files): BOUNDARY = 'PYTHON' CRLF = '\r\n' L = [] for (key, value) in fields: L.append('--' + BOUNDARY) L.append('Content-Disposition: form-data; name="%s"' % key) L.append('Content-Type: text/plain; charset=iso-8859-2') L.append('') L.append(value) for (key, filename, value) in files: L.append('--' + BOUNDARY) L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename)) L.append('Content-Type: %s' % get_content_type(filename)) L.append('') L.append(value) L.append('--' + BOUNDARY + '--') body = CRLF.join(L) content_type = 'multipart/form-data; boundary=%s' % BOUNDARY return content_type, body def post_multipart(host, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) conn = httplib.HTTPConnection(host, 80) conn.putrequest('POST', selector) conn.putheader('Content-type', content_type) conn.putheader('Content-length', str(len(body))) conn.endheaders() conn.send(body) response = conn.getresponse() conn.close() return response if __name__ == '__main__': name = raw_input("\nImage name: ") clipboard = printscreen(name) if clipboard: image_file_body, status = check_file(storage_path + name) if status: fields = [[form_max_name, form_max_value]] files = [[form_file_name, storage_path + name, image_file_body]] response = post_multipart(host, selector, fields, files) if response.status == 200: print 'Upload OK.' else: print 'Error. Upload failed.' else: print 'Error. File not found or too big.' else: print 'Error. Empty clipboard.'