Danh mục

Dive Into Python-Chapter 11. HTTP Web Services

Số trang: 60      Loại file: pdf      Dung lượng: 0.00 B      Lượt xem: 9      Lượt tải: 0    
tailieu_vip

Hỗ trợ phí lưu trữ khi tải xuống: 21,000 VND Tải xuống file đầy đủ (60 trang) 0
Xem trước 6 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu dive into python-chapter 11. http web services, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Dive Into Python-Chapter 11. HTTP Web Services Chapter 11. HTTP Web Services11.1. Diving inYouve learned about HTML processing and XML processing, and along theway you saw how to download a web page and how to parse XML from aURL, but lets dive into the more general topic of HTTP web services.Simply stated, HTTP web services are programmatic ways of sending andreceiving data from remote servers using the operations of HTTP directly. Ifyou want to get data from the server, use a straight HTTP GET; if you wantto send new data to the server, use HTTP POST. (Some more advancedHTTP web service APIs also define ways of modifying existing data anddeleting data, using HTTP PUT and HTTP DELETE.) In other words, the“verbs” built into the HTTP protocol (GET, POST, PUT, and DELETE)map directly to application-level operations for receiving, sending,modifying, and deleting data.The main advantage of this approach is simplicity, and its simplicity hasproven popular with a lot of different sites. Data -- usually XML data -- canbe built and stored statically, or generated dynamically by a server-sidescript, and all major languages include an HTTP library for downloading it.Debugging is also easier, because you can load up the web service in anyweb browser and see the raw data. Modern browsers will even nicely formatand pretty-print XML data for you, to allow you to quickly navigate throughit.Examples of pure XML-over-HTTP web services: * Amazon API allows you to retrieve product information from theAmazon.com online store. * National Weather Service (United States) and Hong Kong Observatory(Hong Kong) offer weather alerts as a web service. * Atom API for managing web-based content. * Syndicated feeds from weblogs and news sites bring you up-to-the-minute news from a variety of sites.In later chapters, youll explore APIs which use HTTP as a transport forsending and receiving data, but dont map application semantics to theunderlying HTTP semantics. (They tunnel everything over HTTP POST.)But this chapter will concentrate on using HTTP GET to get data from aremote server, and youll explore several HTTP features you can use to getthe maximum benefit out of pure HTTP web services.Here is a more advanced version of the openanything module that you sawin the previous chapter:Example 11.1. openanything.pyIf you have not already done so, you can download this and other examplesused in this book.import urllib2, urlparse, gzipfrom StringIO import StringIOUSER_AGENT = OpenAnything/1.0+http://diveintopython.org/http_web_services/class SmartRedirectHandler(urllib2.HTTPRedirectHandler): def http_error_301(self, req, fp, code, msg, headers): result = urllib2.HTTPRedirectHandler.http_error_301( self, req, fp, code, msg, headers) result.status = code return result def http_error_302(self, req, fp, code, msg, headers): result = urllib2.HTTPRedirectHandler.http_error_302( self, req, fp, code, msg, headers) result.status = code return resultclass DefaultErrorHandler(urllib2.HTTPDefaultErrorHandler): def http_error_default(self, req, fp, code, msg, headers): result = urllib2.HTTPError( req.get_full_url(), code, msg, headers, fp) result.status = code return resultdef openAnything(source, etag=None, lastmodified=None,agent=USER_AGENT): URL, filename, or string --> stream This function lets you define parsers that take any input source (URL, pathname to local or network file, or actual data as a string) and deal with it in a uniform manner. Returned object is guaranteed to have all the basic stdio read methods (read, readline, readlines). Just .close() the object when youre done with it. If the etag argument is supplied, it will be used as the value of an If-None-Match request header. If the lastmodified argument is supplied, it must be a formatted date/time string in GMT (as returned in the Last-Modified header of a previous request). The formatted date/time will be usedas the value of an If-Modified-Since request header.If the agent argument is supplied, it will be used as the value of aUser-Agent request header.if hasattr(source, read): return sourceif source == -: return sys.stdinif urlparse.urlparse(source)[0] == http: # open URL with urllib2 request = urllib2.Request(source) request.add_header(User-Agent, agent) if etag: request.add_header(If-None-Match, etag) if lastmodified: request.add_header(If-Modified-Since, lastmodified) request.add_header(Accept-encoding, gzip) opener = urllib2.build_opener(SmartRedirectHandler(),DefaultErrorHandler()) return opener.open(request) # try to open with native open function (if sour ...

Tài liệu được xem nhiều: