Page 102 -
P. 102

textual data


          The program has overloaded

           the Beans’R’Us Server


           It looks like there’s a problem with the program. It’s sending so many
           requests that it overwhelmed the Beans’R’Us website. So why did that
           happen? Let’s look at the code and see:
                                                                                               Here’s the code
                                                                                               as it currently
                                                                                               stands.
               import urllib.request
               price = 99.99
               while price > 4.74:
                   page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")
                   text = page.read().decode("utf8")
                   where = text.find(‘>$’)
                   start_of_price = where + 2
                   end_of_price = start_of_price + 4
                   price = float(text[start_of_price:end_of_price])
               print ("Buy!")





           If the value of price isn’t low enough (if
           it’s more than 4.74), the program goes back
           to the top of the loop immediately and sends
           another request.

           With the code written this way, the program
           will generate thousands of requests per
           hour. Multiply that by all the Starbuzz
           outlets around the world, and you can start
           to see the scale of the problem:


          You need to delay the pricing
           requests. But how?



                                                                                              The Beans 'R' Us
                                                                                               server can't cope
                                                                                               with all the requests.





                                                                                        you are here 4    67
   97   98   99   100   101   102   103   104   105   106   107