Page 143 -
P. 143

create the password variable




                                                This is the start of the program. You were to write a modified
                                                version of this code that will allow the send_to_twitter()
                                                function to see the password variable.
                                                Hint: you might not need to use a function.


                  import urllib.request
                  import time


                   def set_password():
                                                 You needed to rewrite this section.
                      password="C8H10N4O2"


                   set_password()


                   def send_to_twitter(msg):
                      password_manager = urllib.request.HTTPPasswordMgr()
                      password_manager.add_password("Twitter API",
                                     "http://twitter.com/statuses", "starbuzzceo", password)
                      http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
                      page_opener = urllib.request.build_opener(http_handler)
                      urllib.request.install_opener(page_opener)
                      params = urllib.parse.urlencode( {'status': msg} )
                      resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
                      resp.read()









                                                            Because the “password" variable is created
                           This is all you need to do: just
                           create the variable.             outside a function, it is available anywhere
                                                            in the program. The “send_to_twitter()”
                                  password=“C8H10N4O2"      function should now be able to see it.









           108    Chapter 3
   138   139   140   141   142   143   144   145   146   147   148