[Patches] urllib: POST with arbitrary mime type

Fredrik Lundh Fredrik Lundh" <effbot@telia.com
Sat, 22 Apr 2000 16:03:54 +0200


This is a multi-part message in MIME format.

------=_NextPart_000_00BC_01BFAC74.5C77C480
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

as currently implemented, the urllib module allows you to
post with a single content type (x-www-form-urlencoded).

this patch modifies urllib to accept either a string or a
(subclass of) the urllib.RequestBody class.  usage:

    import urllib

    body =3D urllib.RequestBody(mydata, "text/xml")
    result =3D urllib.urlopen(host, body)

or:

    body =3D urllib.RequestBody(name=3D"fredrik", =
email=3D"effbot@telia.com")
    result =3D urllib.urlopen(host, body)

</F>

I confirm that, to the best of my knowledge and belief, this =
contribution
is free of any claims of third parties under copyright, patent or
other rights or interests ("claims").  To the extent that I have
any such claims, I hereby grant to CNRI a nonexclusive, irrevocable,
royalty-free, worldwide license to reproduce, distribute, perform and/or
display publicly, prepare derivative versions, and otherwise use this
contribution as part of the Python software and its related =
documentation,
or any derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.
        =20
I acknowledge that CNRI may, at its sole discretion, decide whether
or not to incorporate this contribution in the Python software and its
related documentation.  I further grant CNRI permission to use my name
and other identifying information provided to CNRI by me for use in
connection with the Python software and its related documentation.


------=_NextPart_000_00BC_01BFAC74.5C77C480
Content-Type: application/octet-stream;
	name="urllib-patch-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="urllib-patch-1"

Index: Lib/urllib.py
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /projects/cvsroot/python/dist/src/Lib/urllib.py,v
retrieving revision 1.91
diff -d -c -r1.91 urllib.py
*** urllib.py	2000/02/04 15:28:41	1.91
--- urllib.py	2000/04/22 14:03:03
***************
*** 69,75 ****
--- 69,93 ----
      if _urlopener:
          _urlopener.cleanup()
 =20
+ class RequestBody:
+     """Class to hold a POST request body"""
+     def __init__(self, body, =
type=3D'application/x-www-form-urlencoded'):
+         self.body =3D body
+         self.type =3D type
+     def gettype(self):
+         return self.type
+     def getbody(self):
+         return self.body
 =20
+ class Form(RequestBody):
+     """Class to hold a POST form request"""
+     def __init__(self, dict, **extra):
+ 	dict =3D dict.copy()
+ 	dict.update(extra)
+ 	RequestBody.__init__(self, dict)
+     def getbody(self):
+ 	return urlencode(self.body)
+=20
  ftpcache =3D {}
  class URLopener:
      """Class to open URLs.
***************
*** 256,264 ****
              auth =3D None
          h =3D httplib.HTTP(host)
          if data is not None:
              h.putrequest('POST', selector)
!             h.putheader('Content-type', =
'application/x-www-form-urlencoded')
!             h.putheader('Content-length', '%d' % len(data))
          else:
              h.putrequest('GET', selector)
          if auth: h.putheader('Authorization', 'Basic %s' % auth)
--- 274,285 ----
              auth =3D None
          h =3D httplib.HTTP(host)
          if data is not None:
+             if type(data) is type(''):
+                 data =3D RequestBody(data)
+             body =3D data.getbody()
              h.putrequest('POST', selector)
!             h.putheader('Content-type', data.gettype())
!             h.putheader('Content-length', str(len(body)))
          else:
              h.putrequest('GET', selector)
          if auth: h.putheader('Authorization', 'Basic %s' % auth)
***************
*** 266,272 ****
          for args in self.addheaders: apply(h.putheader, args)
          h.endheaders()
          if data is not None:
!             h.send(data + '\r\n')
          errcode, errmsg, headers =3D h.getreply()
          fp =3D h.getfile()
          if errcode =3D=3D 200:
--- 287,293 ----
          for args in self.addheaders: apply(h.putheader, args)
          h.endheaders()
          if data is not None:
!             h.send(body + '\r\n')
          errcode, errmsg, headers =3D h.getreply()
          fp =3D h.getfile()
          if errcode =3D=3D 200:

------=_NextPart_000_00BC_01BFAC74.5C77C480--