Advertisement
stuppid_bot

Untitled

Nov 6th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import os
  4. import urllib2
  5. import mimetypes
  6. import uuid
  7.  
  8. class FormData:
  9.     def __init__(self, boundary):
  10.         self.boundary = boundary
  11.         self.out = ''
  12.  
  13.     def add_text(self, name, text):
  14.         self.out += '--%s\r\n' % self.boundary
  15.         self.out += 'Content-Disposition: form-data; name="%s"\r\n\r\n' % name
  16.         self.out += text + '\r\n'
  17.  
  18.     def add_file(self, name, filename):
  19.         content = open(filename, 'rb').read()
  20.         self.out += '--%s\r\n' % self.boundary
  21.         self.out += 'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (name, os.path.basename(filename))
  22.         extension = os.path.splitext(filename)[1]
  23.         self.out += 'Content-Type: %s\r\n\r\n' % (mimetypes.types_map[extension] if extension in mimetypes.types_map else 'application/octet-stream')
  24.         self.out += content + '\r\n'
  25.  
  26.     def __str__(self):
  27.         return self.out + '--' + self.boundary + '--'
  28.  
  29. subject = u'Привет'
  30. message = u'Иди нахуй'
  31. fd = FormData(uuid.uuid4().hex)
  32. fd.add_text('to', 'tz4678@gmail.com')
  33. fd.add_text('subject', subject.encode('utf-8'))
  34. fd.add_text('message', message.encode('utf-8'))
  35. fd.add_file('attachments[]', 'test.txt')
  36. req = urllib2.Request('http://x9a.ru/***.php', str(fd), {'Content-Type': 'multipart/form-data; boundary=' + fd.boundary});
  37. res = urllib2.urlopen(req)
  38. print res.read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement