Jump to content
×
×
  • Create New...

Python Web Import


Denis

Recommended Posts

Hello,

 

While i was surfing at the internet i saw something interesting.

It's a python script which does this work:

You can create a python file in your web space and import it

 

import httplib
import imp
import sys

def register_domain(name):
    WebImporter.registered_domains.add(name)
    parts = reversed(name.split('.'))
    whole = []
    for part in parts:
        whole.append(part)
        WebImporter.domain_modules.add(".".join(whole))

class WebImporter(object):
    domain_modules = set()
    registered_domains = set()

    def find_module(self, fullname, path=None):
        if fullname in self.domain_modules:
            return self
        if fullname.rsplit('.')[0] not in self.domain_modules:
            return None
        try:
            r = self._do_request(fullname, method="HEAD")
        except ValueError:
            return None
        else:
            r.close()
            if r.status == 200:
                return self
        return None

    def load_module(self, fullname):
        if fullname in sys.modules:
            return sys.modules[fullname]
        mod = imp.new_module(fullname)
        mod.__loader__ = self
        sys.modules[fullname] = mod
        if fullname not in self.domain_modules:
            url = "http://%s%s" % self._get_host_and_path(fullname)
            mod.__file__ = url
            r = self._do_request(fullname)
            code = r.read()
            assert r.status == 200
            exec code in mod.__dict__
        else:
            mod.__file__ = "[fake module %r]" % fullname
            mod.__path__ = []
        return mod

    def _do_request(self, fullname, method="GET"):
        host, path = self._get_host_and_path(fullname)
        c = httplib.HTTPConnection(host)
        c.request(method, path)
        return c.getresponse()

    def _get_host_and_path(self, fullname):
        tld, domain, rest = fullname.split('.', 2)
        path = "/%s.py" % rest.replace('.', '/')
        return ".".join([domain, tld]), path

sys.meta_path = [WebImporter()]
your_site = 'dowski.com'
register_domain(your_site)
from com.dowski import test

print "Output from "+your_site+' foo: ', test.foo
print "Output from "+your_site+' bar: ', test.bar

 

Source: http://blog.dowski.com/2008/07/31/customizing-the-python-import-system/

  • Love 5
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Activity

    1. 1

      Client Crash

    2. 14

      CRK - Cronikal [Play now][Official start on April 24, 2022 at 16:00][11 languages available]

    3. 57

      Forever Metin2

    4. 1

      NPC direct item enchant

    5. 37

      Official Mailbox

    6. 0

      [Request] Underground-MT2 Serverfiles, Database , Homepage & Client

    7. 0

      3 countries 3 phrases

    8. 1

      NPC direct item enchant

  • Recently Browsing

    • No registered users viewing this page.

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.