Posted By

felipec on 05/02/07


Tagged

web20 rest


Versions (?)



Who likes this?

8 people have marked this snippet as a favorite

vvarp
jarjar2k7
gedittest
fukami
dbug13
lukaszkorecki
laurenceosx
FSX


Python REST


Published in: Python 



Website Promotion
DIRECTORY
is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Expand | Embed | Plain Text
  1. import httplib
  2. import urlparse
  3. import urllib
  4. import base64
  5.  
  6. class Connection:
  7. def __init__(self, base_url, username, password):
  8. self.base_url = base_url
  9. self.username = username
  10. self.password = password
  11. self.url = urlparse.urlparse(base_url)
  12.  
  13. def request_get(self, resource, args = None):
  14. self.request(resource, "get", args)
  15.  
  16. def request_post(self, resource, args = None):
  17. self.request(resource, "post", args)
  18.  
  19. def request(self, resource, method = "get", args = None):
  20. params = None
  21. path = resource
  22. headers = {}
  23.  
  24. if args:
  25. path += "?" + urllib.urlencode(args)
  26.  
  27. if self.username and self.password:
  28. encoded = base64.encodestring("%s:%s" % (self.username, self.password))[:-1]
  29. headers["Authorization"] = "Basic %s" % encoded
  30.  
  31. if (self.url.port == 443):
  32. conn = httplib.HTTPSConnection(self.url.netloc)
  33. else:
  34. conn = httplib.HTTPConnection(self.url.netloc)
  35.  
  36. req = conn.request(method.upper(), "/" + path, None, headers)
  37.  
  38. r = conn.getresponse()

Report this snippet 

You need to login to post a comment.