/usr/lib/python2.7/dist-packages/pyramid/scripts/prequest.py is in python-pyramid 1.6+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | import base64
import optparse
import sys
import textwrap
from pyramid.compat import url_unquote
from pyramid.request import Request
from pyramid.paster import get_app, setup_logging
from pyramid.scripts.common import parse_vars
def main(argv=sys.argv, quiet=False):
command = PRequestCommand(argv, quiet)
return command.run()
class PRequestCommand(object):
description = """\
Submit a HTTP request to a web application.
This command makes an artifical request to a web application that uses a
PasteDeploy (.ini) configuration file for the server and application.
Use "prequest config.ini /path" to request "/path".
Use "prequest --method=POST config.ini /path < data" to do a POST with
the given request body.
Use "prequest --method=PUT config.ini /path < data" to do a
PUT with the given request body.
Use "prequest --method=PATCH config.ini /path < data" to do a
PATCH with the given request body.
Use "prequest --method=OPTIONS config.ini /path" to do an
OPTIONS request.
Use "prequest --method=PROPFIND config.ini /path" to do a
PROPFIND request.
If the path is relative (doesn't begin with "/") it is interpreted as
relative to "/". The path passed to this script should be URL-quoted.
The path can be succeeded with a query string (e.g. `/path?a=1&=b2').
The variable "environ['paste.command_request']" will be set to "True" in
the request's WSGI environment, so your application can distinguish these
calls from normal requests.
"""
usage = "usage: %prog config_uri path_info [args/options]"
parser = optparse.OptionParser(
usage=usage,
description=textwrap.dedent(description)
)
parser.add_option(
'-n', '--app-name',
dest='app_name',
metavar= 'NAME',
help="Load the named application from the config file (default 'main')",
type="string",
)
parser.add_option(
'--header',
dest='headers',
metavar='NAME:VALUE',
type='string',
action='append',
help="Header to add to request (you can use this option multiple times)"
)
parser.add_option(
'-d', '--display-headers',
dest='display_headers',
action='store_true',
help='Display status and headers before the response body'
)
parser.add_option(
'-m', '--method',
dest='method',
choices=['GET', 'HEAD', 'POST', 'PUT', 'PATCH','DELETE',
'PROPFIND', 'OPTIONS'],
type='choice',
help='Request method type (GET, POST, PUT, PATCH, DELETE, '
'PROPFIND, OPTIONS)',
)
parser.add_option(
'-l', '--login',
dest='login',
type='string',
help='HTTP basic auth username:password pair',
)
get_app = staticmethod(get_app)
stdin = sys.stdin
def __init__(self, argv, quiet=False):
self.quiet = quiet
self.options, self.args = self.parser.parse_args(argv[1:])
def out(self, msg): # pragma: no cover
if not self.quiet:
print(msg)
def configure_logging(self, app_spec):
setup_logging(app_spec)
def run(self):
if not len(self.args) >= 2:
self.out('You must provide at least two arguments')
return 2
app_spec = self.args[0]
path = self.args[1]
self.configure_logging(app_spec)
if not path.startswith('/'):
path = '/' + path
try:
path, qs = path.split('?', 1)
except ValueError:
qs = ''
path = url_unquote(path)
headers = {}
if self.options.login:
enc = base64.b64encode(self.options.login.encode('ascii'))
headers['Authorization'] = 'Basic ' + enc.decode('ascii')
if self.options.headers:
for item in self.options.headers:
if ':' not in item:
self.out(
"Bad --header=%s option, value must be in the form "
"'name:value'" % item)
return 2
name, value = item.split(':', 1)
headers[name] = value.strip()
app = self.get_app(app_spec, self.options.app_name,
options=parse_vars(self.args[2:]))
request_method = (self.options.method or 'GET').upper()
environ = {
'REQUEST_METHOD': request_method,
'SCRIPT_NAME': '', # may be empty if app is at the root
'PATH_INFO': path,
'SERVER_NAME': 'localhost', # always mandatory
'SERVER_PORT': '80', # always mandatory
'SERVER_PROTOCOL': 'HTTP/1.0',
'CONTENT_TYPE': 'text/plain',
'REMOTE_ADDR':'127.0.0.1',
'wsgi.run_once': True,
'wsgi.multithread': False,
'wsgi.multiprocess': False,
'wsgi.errors': sys.stderr,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0),
'QUERY_STRING': qs,
'HTTP_ACCEPT': 'text/plain;q=1.0, */*;q=0.1',
'paste.command_request': True,
}
if request_method in ('POST', 'PUT', 'PATCH'):
environ['wsgi.input'] = self.stdin
environ['CONTENT_LENGTH'] = '-1'
for name, value in headers.items():
if name.lower() == 'content-type':
name = 'CONTENT_TYPE'
else:
name = 'HTTP_'+name.upper().replace('-', '_')
environ[name] = value
request = Request.blank(path, environ=environ)
response = request.get_response(app)
if self.options.display_headers:
self.out(response.status)
for name, value in response.headerlist:
self.out('%s: %s' % (name, value))
if response.charset:
self.out(response.ubody)
else:
self.out(response.body)
return 0
if __name__ == '__main__': # pragma: no cover
sys.exit(main() or 0)
|