proxy.py :  » GUI » BloGTK » BloGTK-1.1 » src » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » BloGTK 
BloGTK » BloGTK 1.1 » src » proxy.py
#!/usr/bin/env python

# 0.9 - This code has been sent by Michael Twomey <mick@enginesofcreation.ie> and adds support for basic HTTP proxies in BloGTK.

from xmlrpclib import Transport,Server

class ProxyTransport(Transport):
  """Handles an HTTP transaction to an XML-RPC server, through an HTTP proxy."""

  def __init__(self, host, port):
    self.proxyHost = host
    self.proxyPort = port

  def request(self, host, handler, request_body, verbose=0):
    return Transport.request(self, host, "http://"+host+handler, request_body, verbose)

  def make_connection(self, host):
    import httplib
    return httplib.HTTP(self.proxyHost, self.proxyPort)
import os
import urlparse
import xmlrpclib
import ConfigParser

def get_xmlrpc_server(url, server=xmlrpclib.Server):

        # 0.9 - Here's where we pull our config values for determining what post method to use.
  option = ""
        homeDir = os.path.expanduser('~')
  proxyConfig = homeDir + "/.BloGTK/proxies.conf"
  proxyParser = ConfigParser.ConfigParser()
  # 0.9-1 - Apparently this will die if there's no proxies.conf - which is not what we want.
  # Therefore, we'll create a new file should one not exist.
  try:
     proxyFile = open(proxyConfig, "rw")
     proxyParser.readfp(proxyFile)
  except:
     proxyFile = open(proxyConfig, "w")
     proxyParser.add_section("Proxy")

     proxyParser.set("Proxy", "port", "80")
     proxyParser.set("Proxy", "server", "proxy.yourserver.com")
     proxyParser.set("Proxy", "option", "none")
     proxyParser.write(proxyFile)
           proxyFile.close()

  try:
     custom_port = proxyParser.get("Proxy", "port")
           custom_host = proxyParser.get("Proxy", "server")
           option = proxyParser.get("Proxy", "option")
  except:
     pass

  if option == "system":
     try:
        (scheme, host, path, parameters, query, frag) = urlparse.urlparse(os.environ["http_proxy"])
              (hostname, port) = host.split(":", 1)

        return server(url, transport=ProxyTransport(hostname, int(port)))
     except:
        print "PROXY ERROR: Could not retrieve system value http_proxy."
        # 0.9 - TODO - Better error handling including a dialog would be nice here.
  if option == "custom":
     return server(url, transport=ProxyTransport(custom_host, int(custom_port)))
  else:
     return server(url)

  proxyFile.close()

# 0.9 - This code can be scrapped
if "__main__" == __name__:

  print "Without proxy: ",
  try:
    betty = Server("http://betty.userland.com")
    print betty.examples.getStateName(41)
  except Exception, e:
    print "Oops, %s: %s" % (e.__class__.__name__, e)

  print "With proxy: ",
  try:
    betty = Server("http://betty.userland.com", transport=ProxyTransport("webcache.uk.sun.com", 8080))
    print betty.examples.getStateName(41)
  except Exception, e:
    print "Oops, %s: %s" % (e.__class__.__name__, e)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.