home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / u / urllib < prev    next >
Text File  |  1996-11-14  |  5KB  |  96 lines

  1. <TITLE>urllib -- Python library reference</TITLE>
  2. Next: <A HREF="../h/httplib" TYPE="Next">httplib</A>  
  3. Prev: <A HREF="../c/cgi" TYPE="Prev">cgi</A>  
  4. Up: <A HREF="../i/internet_and_www" TYPE="Up">Internet and WWW</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H1>10.2. Standard Module <CODE>urllib</CODE></H1>
  7. This module provides a high-level interface for fetching data across
  8. the World-Wide Web.  In particular, the <CODE>urlopen</CODE> function is
  9. similar to the built-in function <CODE>open</CODE>, but accepts URLs
  10. (Universal Resource Locators) instead of filenames.  Some restrictions
  11. apply --- it can only open URLs for reading, and no seek operations
  12. are available.
  13. <P>
  14. it defines the following public functions:
  15. <P>
  16. <DL><DT><B>urlopen</B> (<VAR>url</VAR>) -- function of module urllib<DD>
  17. Open a network object denoted by a URL for reading.  If the URL does
  18. not have a scheme identifier, or if it has `<SAMP>file:</SAMP>' as its scheme
  19. identifier, this opens a local file; otherwise it opens a socket to a
  20. server somewhere on the network.  If the connection cannot be made, or
  21. if the server returns an error code, the <CODE>IOError</CODE> exception is
  22. raised.  If all went well, a file-like object is returned.  This
  23. supports the following methods: <CODE>read()</CODE>, <CODE>readline()</CODE>,
  24. <CODE>readlines()</CODE>, <CODE>fileno()</CODE>, <CODE>close()</CODE> and <CODE>info()</CODE>.
  25. Except for the last one, these methods have the same interface as for
  26. file objects --- see the section on File Objects earlier in this
  27. manual.  (It's not a built-in file object, however, so it can't be
  28. used at those few places where a true built-in file object is
  29. required.)
  30. <P>
  31. The <CODE>info()</CODE> method returns an instance of the class
  32. <CODE>rfc822.Message</CODE> containing the headers received from the server,
  33. if the protocol uses such headers (currently the only supported
  34. protocol that uses this is HTTP).  See the description of the
  35. <CODE>rfc822</CODE> module.
  36. </DL>
  37. <DL><DT><B>urlretrieve</B> (<VAR>url</VAR>) -- function of module urllib<DD>
  38. Copy a network object denoted by a URL to a local file, if necessary.
  39. If the URL points to a local file, or a valid cached copy of the
  40. object exists, the object is not copied.  Return a tuple (<VAR>filename</VAR>,
  41. <VAR>headers</VAR>) where <VAR>filename</VAR> is the local file name under which
  42. the object can be found, and <VAR>headers</VAR> is either <CODE>None</CODE> (for
  43. a local object) or whatever the <CODE>info()</CODE> method of the object
  44. returned by <CODE>urlopen()</CODE> returned (for a remote object, possibly
  45. cached).  Exceptions are the same as for <CODE>urlopen()</CODE>.
  46. </DL>
  47. <DL><DT><B>urlcleanup</B> () -- function of module urllib<DD>
  48. Clear the cache that may have been built up by previous calls to
  49. <CODE>urlretrieve()</CODE>.
  50. </DL>
  51. <DL><DT><B>quote</B> (<VAR>string</VAR>[, <VAR>addsafe</VAR>]) -- function of module urllib<DD>
  52. Replace special characters in <VAR>string</VAR> using the <CODE>%xx</CODE> escape.
  53. Letters, digits, and the characters ``<CODE>_,.-</CODE>'' are never quoted.
  54. The optional <VAR>addsafe</VAR> parameter specifies additional characters
  55. that should not be quoted --- its default value is <CODE>'/'</CODE>.
  56. <P>
  57. Example: <CODE>quote('/~conolly/')</CODE> yields <CODE>'/%7econnolly/'</CODE>.
  58. </DL>
  59. <DL><DT><B>unquote</B> (<VAR>string</VAR>) -- function of module urllib<DD>
  60. Replace `<SAMP>%xx</SAMP>' escapes by their single-character equivalent.
  61. <P>
  62. Example: <CODE>unquote('/%7Econnolly/')</CODE> yields <CODE>'/~connolly/'</CODE>.
  63. </DL>
  64. Restrictions:
  65. <P>
  66. <UL>
  67. <LI>•  Currently, only the following protocols are supported: HTTP, (versions
  68. 0.9 and 1.0), Gopher (but not Gopher-+), FTP, and local files.
  69. <LI>•  The caching feature of <CODE>urlretrieve()</CODE> has been disabled until I
  70. find the time to hack proper processing of Expiration time headers.
  71. <P>
  72. <LI>•  There should be a function to query whether a particular URL is in
  73. the cache.
  74. <P>
  75. <LI>•  For backward compatibility, if a URL appears to point to a local file
  76. but the file can't be opened, the URL is re-interpreted using the FTP
  77. protocol.  This can sometimes cause confusing error messages.
  78. <P>
  79. <LI>•  The <CODE>urlopen()</CODE> and <CODE>urlretrieve()</CODE> functions can cause
  80. arbitrarily long delays while waiting for a network connection to be
  81. set up.  This means that it is difficult to build an interactive
  82. web client using these functions without using threads.
  83. <P>
  84. <LI>•  The data returned by <CODE>urlopen()</CODE> or <CODE>urlretrieve()</CODE> is the
  85. raw data returned by the server.  This may be binary data (e.g. an
  86. image), plain text or (for example) HTML.  The HTTP protocol provides
  87. type information in the reply header, which can be inspected by
  88. looking at the <CODE>Content-type</CODE> header.  For the Gopher protocol,
  89. type information is encoded in the URL; there is currently no easy way
  90. to extract it.  If the returned data is HTML, you can use the module
  91. <CODE>htmllib</CODE> to parse it.
  92. <LI>•  Although the <CODE>urllib</CODE> module contains (undocumented) routines to
  93. parse and unparse URL strings, the recommended interface for URL
  94. manipulation is in module <CODE>urlparse</CODE>.
  95. </UL>
  96.