Advertisement
stuppid_bot

Untitled

Mar 9th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. import uuid, mimetypes, os
  2.  
  3. CRLF = b'\r\n'
  4. TWO_HYPHENS = b'--'
  5. DEFAULT_FILE_CONTENT_TYPE = 'application/octeat-stream'
  6.  
  7. class MultipartBuilder(object):
  8.     def __init__(self, encoding='utf-8'):
  9.         self._encoding = encoding
  10.         self._boundary = uuid.uuid4().hex
  11.         self._contentType = \
  12.         "multipart/form-data; boundary={}".format(self.boundary)
  13.         self._boundaryBytes = self.boundary.encode("utf-8")
  14.         self._dashBoundary = TWO_HYPHENS + self._boundaryBytes
  15.         self._separatorLine = self._dashBoundary + CRLF
  16.         self._endBody = self._dashBoundary + TWO_HYPHENS + CRLF + CRLF
  17.         self._body = b''
  18.  
  19.     @property
  20.     def encoding(self):
  21.         return self._encoding
  22.  
  23.     @property
  24.     def boundary(self):
  25.         return self._boundary
  26.  
  27.     @property
  28.     def contentType(self):
  29.         return self._contentType
  30.  
  31.     @property
  32.     def data(self):
  33.         return self.build()
  34.  
  35.     def _quoteEncode(self, s):
  36.         return '"{}"'.format(s.replace('\n', '%0A').replace('\r', '%0D')\
  37.             .replace('"', '%22')).encode(self.encoding, 'replace')
  38.  
  39.     def _addBodyPart(self, name, content, filename, contentType):
  40.         assert isinstance(content, bytes), "must be bytes"
  41.        
  42.         self._body += self._separatorLine
  43.         self._body += b'Content-Disposition: form-data; name=' + \
  44.                       self._quoteEncode(name)
  45.  
  46.         if filename:
  47.             self._body += b'; filename=' + self._quoteEncode(filename)
  48.  
  49.         self._body += CRLF
  50.  
  51.         if contentType:
  52.             self._body += \
  53.             bytes('Content-Type: {0}'.format(contentType), 'utf-8') + CRLF
  54.            
  55.         self._body += CRLF + content + CRLF
  56.  
  57.     def addField(self, name, content):
  58.         if isinstance(content, str):
  59.             content = bytes(content, self.encoding, 'replace')
  60.            
  61.         self._addBodyPart(name, content, None, None)
  62.  
  63.     def addFile(self, name, filename, content=None,
  64.                 contentType=None):
  65.         if content is None:
  66.             fp = open(filename, 'rb')
  67.             content = fp.read()
  68.             fp.close()
  69.         elif hasattr(content, 'read'):
  70.             content = content.read()
  71.  
  72.         filename = os.path.basename(filename)
  73.  
  74.         if contentType is None:
  75.             contentType = mimetypes.guess_type(filename)[0]
  76.                
  77.         self._addBodyPart(name, content, filename,
  78.                           contentType or DEFAULT_FILE_CONTENT_TYPE)
  79.  
  80.     def getBody(self):
  81.         return self._body + self._endBody
  82.  
  83.  
  84. if __name__ == '__main__':
  85.     builder = MultipartBuilder('windows-1251')
  86.     builder.addFile("wmphoto", "Текстовый файл.txt",
  87.         "Какой-то текст.".encode("utf-8"))
  88.     builder.addField("Submit", "Отправить")
  89.     requestBody = builder.getBody()
  90.     url = "http://chilehead.us/waymarking/test_upload.php"
  91.     from urllib.request import Request, urlopen
  92.     req = Request(url, requestBody, {'Content-Type': builder.contentType})
  93.     resp = urlopen(req)
  94.     html = str(resp.read(), 'windows-1251')
  95.     print( html )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement