예제 #1
0
    def webquery(self, args=(), **kw):
        """Write output of a specified URL to stdout or file.
        Keywords for query may be specified as a sequence of pairs in
        args or as keywords.  Special keywords that define the URL include:
        host (default 'localhost'), url (default null), method (default 'POST'),
        and port (default 80).  The file keyword specifies an output filename
        or file handle (default sys.stdout).  Additional keywords are passed
        as parameters to the query.
            This method swiped in toto from Rick.
        """

        args = list(args)
        for key, value in kw.items():
            args.append((key, value))
        port = 80
        method = "POST"
        url = ""
        host = urllib.localhost()
        outfile = sys.stdout
        query = []
        for key, value in args:
            if key == "port":
                port = int(value)
            elif key == "method":
                method = value.upper()
            elif key == "url":
                url = value
            elif key == "host":
                host = value
            elif key == "file":
                outfile = value
            elif value is None:
                query.append(urllib.quote(key))
            else:
                query.append(
                    '%s=%s' %
                    (urllib.quote(key), urllib.quote_plus(str(value))))
        query = '&'.join(query)
        if isinstance(outfile, types.StringType):
            outfile = open(outfile, "w")
        if url[:1] == "/":
            # don't add an extra slash (purely for aesthetic purposes)
            url = "http://%s:%d%s" % (host, port, url)
        else:
            url = "http://%s:%d/%s" % (host, port, url)
        if not query:
            query = None
        elif method == "GET":
            url = "%s?%s" % (url, query)
            query = None
        inurl = urllib.urlopen(url, query)
        print url, query
        s = inurl.read(102400)
        while s:
            outfile.write(s)
            s = inurl.read(102400)
예제 #2
0
                raise IOError(e.errno, e.strerror, e.filename)
            size = stats.st_size
            modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
            mtype = mimetypes.guess_type(url)[0]
            headers = mimetools.Message(StringIO(
                'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
                (mtype or 'text/html; charset=utf-8;', size, modified)))
        if not host:
            urlfile = file
            if file[:1] == '/':
                urlfile = 'file://' + file
            return urllib.addinfourl(OpenOnRead(localname, 'rb'),
                              headers, urlfile)
        host, port = urllib.splitport(host)
        if not port \
           and socket.gethostbyname(host) in (urllib.localhost(), urllib.thishost()):
            urlfile = file
            if file[:1] == '/':
                urlfile = 'file://' + file
            return urllib.addinfourl(OpenOnRead(localname, 'rb'),
                              headers, urlfile)
        raise IOError, ('local file error', 'not on local host')

    def dirlisting(selfself, path):
        try:
            names = os.listdir(path)
        except os.error, msg:
            exc_type, exc_value, exc_tb = sys.exc_info()
            raise IOError, msg, exc_tb
        names.sort()
        s = MyStringIO("file:"+path, {'content-type': 'text/html'})
예제 #3
0
def myip(x):
    print "LOCAL HOST=%s" % (urllib.localhost())
    print "IP=%s" % (socket.gethostbyname(x))
예제 #4
0
def myip(x):
	print "LOCAL HOST=%s"% (urllib.localhost())
	print "IP=%s"% (socket.gethostbyname(x))
예제 #5
0
def webquery_open(args=(), **kw):
    """
    Return a read-only file descriptor to read the results of a web
    query.

    Keywords for query may be specified as a sequence of pairs in args
    or as keywords.  Special keywords that define the URL include:
    host (default 'localhost'), url (default null), method (default
    'POST'), port (default 80), timeout (default None).  Additional
    keywords are passed as parameters to the query.

    If a parameter keyword has a list as its value, the parameter is
    included multiple times in the query, once for each argument.
    """

    args = list(args)
    for key, value in kw.iteritems():
        args.append((key,value))
    port = 80
    method = "POST"
    url = ""
    host = urllib.localhost()
    query = []
    timeout = None
    for key, value in args:
        if key == "port":
            port = int(value)
        elif key == "method":
            method = value.upper()
        elif key == "url":
            url = value
        elif key == "host":
            host = value
        elif key == "timeout":
            timeout = value
        elif value is None:
            query.append(urllib.quote(key))
        elif isinstance(value,list):
            qkey = urllib.quote(key)
            for v in value:
                query.append('%s=%s' % (qkey, urllib.quote_plus(str(v))))
        else:
            query.append('%s=%s' %
                         (urllib.quote(key), urllib.quote_plus(str(value))))
    query = '&'.join(query)

    if url[:1] == "/":
        # don't add an extra slash (purely for aesthetic purposes)
        url = "http://%s:%d%s" % (host,port,url)
    else:
        url = "http://%s:%d/%s" % (host,port,url)

    if not query:
        query = None
    elif method == "GET":
        url = "%s?%s" % (url,query)
        query = None

    if URLLIB2_HAS_TIMEOUT:
        return urllib2.urlopen(url, query, timeout)
    else:
        # This is the old way to set a socket timeout prior to Python
        # 2.6.  NOTE THIS IS NOT THREADSAFE
        old_timeout = socket.getdefaulttimeout()
        socket.setdefaulttimeout(timeout)
        try:
            req = urllib2.urlopen(url, query)
        finally:
            socket.setdefaulttimeout(old_timeout)
        return req
예제 #6
0
import unittest
from app import conf, flapp
import urllib
import argparse

from threading import Thread

if __name__ == "__main__":
	parser = argparse.ArgumentParser()
	parser.add_argument('--env', choices = ('test', 'dev', 'prod'), default = 'dev', help = 'Specify environment, which determines which database to use.', type = str)
	parser.add_argument('--host', default = urllib.localhost(), help = 'Specify which host to run tests against', type = str)
	parser.add_argument('--port', default = '8080', type = int)
	args = parser.parse_args()

	# root_url = 'http://%s:%s'%(args.host, args.port)

	conf.config_test_env(flapp, HOST = args.host, PORT = args.port)
	unittest.main()
예제 #7
0
def handleConnection(clientSocket):
    [method, url, protocol], remaining = parseFirstMessageFrom(clientSocket)
    if method == 'CONNECT':
        host, port, path = parseURL(url)
        if (isBlocked(host)):
            clientSocket.send(HTTP_VERSION + ' 403 Forbidden\r\n\r\n')
            clientSocket.close()
            return
        else:    
            serverSocket = getServerSocket(host, port)
            clientSocket.send(HTTP_VERSION + ' 200 Connection established\n' + 'Proxy-agent: %s\r\n\r\n' % (PROXY_AGENT))
        
    elif method in ('OPTIONS', 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'TRACE'):
        host, port, path = parseURL(url[7:])
        serverSocket = getServerSocket(host, port)
        serverSocket.send('%s %s %s' % (method, path, protocol) + remaining)
    
    forwardMessagesBetween(clientSocket, serverSocket)
    
    clientSocket.close()
    serverSocket.close()

if __name__ == '__main__':
    proxySocket = socket(AF_INET, SOCK_STREAM)
    proxySocket.bind((localhost(), PROXY_PORT))
    print "Tutanlar's HTTP Proxy Server is listening on %s:%d." % (localhost(), PROXY_PORT)
    proxySocket.listen(BACKLOG)
    while 1:
        start_new_thread(handleConnection, (proxySocket.accept()[0],))