本文整理汇总了Python中sitescripts.utils.setupStderr函数的典型用法代码示例。如果您正苦于以下问题:Python setupStderr函数的具体用法?Python setupStderr怎么用?Python setupStderr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setupStderr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handleRequest
def handleRequest(environ, start_response):
setupStderr(environ['wsgi.errors'])
if not environ.get('HTTP_X_ADBLOCK_PLUS'):
return showError('Please use Adblock Plus to submit crashes', start_response)
if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYPE', '').startswith('text/xml'):
return showError('Unsupported request method', start_response)
params = parse_qs(environ.get('QUERY_STRING', ''))
requestVersion = params.get('version', ['0'])[0]
if requestVersion != '1':
return showError('Unsupported request version', start_response)
try:
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
except (ValueError):
return showError('No content', start_response)
dir = get_config().get('crashes', 'dataPath')
if not os.path.exists(dir):
os.makedirs(dir)
filename = None
try:
fd, filename = mkstemp('.xml.tmp', 'crash_', dir)
file = os.fdopen(fd, 'wb')
file.write(environ['wsgi.input'].read(request_body_size))
file.close()
os.rename(filename, os.path.splitext(filename)[0]);
except Exception, e:
if filename != None and os.path.isfile(filename):
os.remove(filename)
raise e
示例2: handleRequest
def handleRequest(environ, start_response):
setupStderr(environ['wsgi.errors'])
start_response('200 OK', [('Content-Type', 'text/plain; charset=utf-8')])
if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'):
return 'Unsupported request method'
try:
request_body_length = int(environ['CONTENT_LENGTH'])
except:
return 'Invalid or missing Content-Length header'
request_body = environ['wsgi.input'].read(request_body_length)
params = {}
for key, value in parse_qsl(request_body):
params[key] = value.decode('utf-8').strip()
if not 'name' in params or params['name'] == '':
return 'No name entered'
if not 'email' in params or params['email'] == '':
return 'No email address entered'
if not 'subject' in params or params['subject'] == '':
return 'No subject entered'
if not 'message' in params or params['message'] == '':
return 'No message entered'
if not re.match(r'^\w[\w.+!-][email protected]\w[\w.-]+\.[a-zA-Z]{2,6}$', params['email']):
return 'Invalid email address'
sendMail(get_config().get('formmail', 'template'), params)
return 'Message sent'
示例3: query_handler
def query_handler(environ, start_response):
setupStderr(environ["wsgi.errors"])
params = dict(parse_qsl(environ.get("QUERY_STRING", "")))
try:
db_connection = db.connect()
try:
results = db.query(db_connection, *query(**params), dict_result=True)
total = db.query(db_connection, "SELECT FOUND_ROWS()")[0][0]
finally:
db_connection.close()
except MySQLdb.Error:
traceback.print_exc()
return common.show_error("Failed to query database!", start_response,
"500 Database error")
try:
echo = int(params["echo"])
except (ValueError, KeyError):
echo = 0
response_headers = [("Content-type", "application/json; charset=utf-8")]
start_response("200 OK", response_headers)
return [json.dumps({"results": results, "echo": echo,
"total": total, "count": len(results)},
ensure_ascii=False).encode("utf-8")]
示例4: submit_data
def submit_data(environ, start_response):
setupStderr(environ["wsgi.errors"])
if environ["REQUEST_METHOD"].upper() != "POST":
return showError("Unsupported request method", start_response)
params = parse_qs(environ.get("QUERY_STRING", ""))
requestVersion = params.get("version", ["0"])[0]
data = "{}"
try:
data_length = int(environ.get("CONTENT_LENGTH", "0"))
except ValueError:
data_length = 0
if data_length != 0:
data = environ["wsgi.input"].read(data_length)
try:
data = json.loads(data)
except json.decoder.JSONDecodeError:
return showError("Error while parsing JSON data.", start_response)
db = _get_db()
for domain, status in data.iteritems():
process_domain(db, domain, status)
db.commit()
response_headers = [("Content-type", "text/plain")]
start_response("200 OK", response_headers)
return []
示例5: submit_data
def submit_data(environ, start_response):
setupStderr(environ['wsgi.errors'])
if environ['REQUEST_METHOD'].upper() != 'POST':
return showError('Unsupported request method', start_response)
params = parse_qs(environ.get('QUERY_STRING', ''))
requestVersion = params.get('version', ['0'])[0]
data = '{}'
try:
data_length = int(environ.get('CONTENT_LENGTH', '0'))
except ValueError:
data_length = 0
if data_length != 0:
data = environ['wsgi.input'].read(data_length)
try:
data = json.loads(data)
except json.decoder.JSONDecodeError:
return showError('Error while parsing JSON data.', start_response)
db = _get_db()
for domain, status in data.iteritems():
process_domain(db, domain, status)
db.commit()
response_headers = [('Content-type', 'text/plain')]
start_response('200 OK', response_headers)
return []
示例6: hook
def hook(ui=None, repo=None, **kwargs):
setupStderr()
root = repo.root if repo != None else get_config().get('hg', 'auth_repository')
result = generate_data(root)
with open(get_config().get('hg', 'auth_file'), 'wb') as file:
for s in result:
file.write(s)
示例7: handleRequest
def handleRequest(environ, start_response):
setupStderr(environ['wsgi.errors'])
if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'):
return showError('Unsupported request method', start_response)
try:
request_body_length = int(environ['CONTENT_LENGTH'])
except:
return showError('Invalid or missing Content-Length header', start_response)
request_body = environ['wsgi.input'].read(request_body_length)
params = {}
for key, value in parse_qsl(request_body):
params[key] = value.decode('utf-8')
guid = params.get('guid', '').lower()
if not re.match(r'^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$', guid):
return showError('Invalid or missing report GUID', start_response)
reportData = getReport(guid)
if reportData == None:
return showError('Report does not exist', start_response)
secret = calculateReportSecret(guid)
if params.get('secret', '') != secret and params.get('secret', '') != calculateReportSecret_compat(guid):
return showError('Wrong secret value', start_response)
reportData['status'] = params.get('status', '')
if len(reportData['status']) > 1024:
reportData['status'] = reportData['status'][:1024]
oldusefulness = reportData.get('usefulness', '0')
reportData['usefulness'] = params.get('usefulness', '0')
if ('email' in reportData):
updateUserUsefulness(getUserId(reportData['email']), reportData['usefulness'], oldusefulness)
saveReport(guid, reportData)
if params.get('notify', '') and 'email' in reportData:
email = reportData['email']
email = re.sub(r' at ', r'@', email)
email = re.sub(r' dot ', r'.', email)
if re.match(r'^[\w.%+-][email protected][\w.%+-]+(\.[\w.%+-]+)+', email):
sendUpdateNotification({
'email': email,
'url': get_config().get('reports', 'urlRoot') + guid,
'status': reportData['status'],
})
newURL = get_config().get('reports', 'urlRoot') + guid
newURL += '?updated=' + str(int(random.uniform(0, 10000)))
newURL += '#secret=' + secret
start_response('302 Found', [('Location', newURL.encode('utf-8'))])
return []
示例8: handleRequest
def handleRequest(environ, start_response):
setupStderr(environ["wsgi.errors"])
if environ["REQUEST_METHOD"].upper() != "POST" or not environ.get("CONTENT_TYPE", "").startswith(
"application/x-www-form-urlencoded"
):
return showError("Unsupported request method", start_response)
try:
request_body_length = int(environ["CONTENT_LENGTH"])
except:
return showError("Invalid or missing Content-Length header", start_response)
request_body = environ["wsgi.input"].read(request_body_length)
params = {}
for key, value in parse_qsl(request_body):
params[key] = value.decode("utf-8")
guid = params.get("guid", "").lower()
if not re.match(r"^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$", guid):
return showError("Invalid or missing report GUID", start_response)
reportData = getReport(guid)
if reportData == None:
return showError("Report does not exist", start_response)
secret = calculateReportSecret(guid)
if params.get("secret", "") != secret and params.get("secret", "") != calculateReportSecret_compat(guid):
return showError("Wrong secret value", start_response)
reportData["status"] = params.get("status", "")
if len(reportData["status"]) > 1024:
reportData["status"] = reportData["status"][:1024]
oldusefulness = reportData.get("usefulness", "0")
reportData["usefulness"] = params.get("usefulness", "0")
if "email" in reportData:
updateUserUsefulness(getUserId(reportData["email"]), reportData["usefulness"], oldusefulness)
saveReport(guid, reportData)
if params.get("notify", "") and "email" in reportData:
email = reportData["email"]
email = re.sub(r" at ", r"@", email)
email = re.sub(r" dot ", r".", email)
if re.match(r"^[\w.%+-][email protected][\w.%+-]+(\.[\w.%+-]+)+", email):
sendUpdateNotification(
{"email": email, "url": get_config().get("reports", "urlRoot") + guid, "status": reportData["status"]}
)
newURL = get_config().get("reports", "urlRoot") + guid
newURL += "?updated=" + str(int(random.uniform(0, 10000)))
newURL += "#secret=" + secret
start_response("302 Found", [("Location", newURL.encode("utf-8"))])
return []
示例9: handleRequest
def handleRequest(environ, start_response):
setupStderr(environ["wsgi.errors"])
params = parse_qs(environ.get("QUERY_STRING", ""))
id = params.get("id", [""])[0].lower()
if not re.match(r"^[\da-f]{32}$", id):
return showError("Invalid or missing ID", start_response)
thisweek = getDigestSecret(id, date.today().isocalendar())
prevweek = getDigestSecret(id, (date.today() - timedelta(weeks=1)).isocalendar())
thisweek_compat = getDigestSecret_compat(id, date.today().isocalendar())
prevweek_compat = getDigestSecret_compat(id, (date.today() - timedelta(weeks=1)).isocalendar())
redirect = False
secret = params.get("secret", [""])[0].lower()
if secret:
redirect = True
else:
try:
cookies = Cookie.SimpleCookie(environ.get("HTTP_COOKIE", ""))
secret = cookies[id].value
except (Cookie.CookieError, KeyError):
return showError("No digest secret", start_response)
if secret != thisweek and secret != prevweek and secret != thisweek_compat and secret != prevweek_compat:
return showError("Wrong secret", start_response)
path = os.path.join(get_config().get("reports", "digestPath"), id + ".html")
if not os.path.exists(path):
return showError("Digest doesn't exist", start_response)
cookies = Cookie.SimpleCookie()
cookies[id] = secret
cookies[id]["path"] = "/"
cookies[id]["secure"] = True
cookies[id]["httponly"] = True
expiration = datetime.utcnow() + timedelta(weeks=2)
cookies[id]["expires"] = expiration.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
if redirect:
start_response("302 Found", [("Location", "/digest?id=" + id), ("Set-Cookie", cookies[id].OutputString())])
return []
else:
start_response(
"200 OK", [("Content-Type", "text/html; charset=utf-8"), ("Set-Cookie", cookies[id].OutputString())]
)
blockSize = 4096
f = open(path)
if "wsgi.file_wrapper" in environ:
return environ["wsgi.file_wrapper"](f, blockSize)
else:
return iter(lambda: f.read(blockSize), "")
示例10: handleRequest
def handleRequest(environ, start_response):
setupStderr(environ['wsgi.errors'])
params = parse_qs(environ.get('QUERY_STRING', ''))
id = params.get('id', [''])[0].lower()
if not re.match(r'^[\da-f]{32}$', id):
return showError('Invalid or missing ID', start_response)
thisweek = getDigestSecret(id, date.today().isocalendar())
prevweek = getDigestSecret(id, (date.today() - timedelta(weeks=1)).isocalendar())
thisweek_compat = getDigestSecret_compat(id, date.today().isocalendar())
prevweek_compat = getDigestSecret_compat(id, (date.today() - timedelta(weeks=1)).isocalendar())
redirect = False
secret = params.get('secret', [''])[0].lower()
if secret:
redirect = True
else:
try:
cookies = Cookie.SimpleCookie(environ.get('HTTP_COOKIE', ''))
secret = cookies[id].value
except (Cookie.CookieError, KeyError):
return showError('No digest secret', start_response)
if secret != thisweek and secret != prevweek and secret != thisweek_compat and secret != prevweek_compat:
return showError('Wrong secret', start_response)
path = os.path.join(get_config().get('reports', 'digestPath'), id + '.html')
if not os.path.exists(path):
return showError("Digest doesn't exist", start_response)
cookies = Cookie.SimpleCookie()
cookies[id] = secret
cookies[id]['path'] = '/'
cookies[id]['secure'] = True
cookies[id]['httponly'] = True
expiration = datetime.utcnow() + timedelta(weeks=2)
cookies[id]['expires'] = expiration.strftime('%a, %d-%b-%Y %H:%M:%S GMT')
if redirect:
start_response('302 Found', [('Location', '/digest?id=' + id), ('Set-Cookie', cookies[id].OutputString())])
return []
else:
start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8'), ('Set-Cookie', cookies[id].OutputString())])
blockSize = 4096
f = open(path)
if 'wsgi.file_wrapper' in environ:
return environ['wsgi.file_wrapper'](f, blockSize)
else:
return iter(lambda: f.read(blockSize), '')
示例11: submit
def submit(environ, start_response):
setupStderr(environ["wsgi.errors"])
config = get_config()
# Check that this is a POST request
if environ["REQUEST_METHOD"] != "POST":
return common.show_error("Unsupported request method", start_response)
# Parse the submitted JSON
try:
data = json.loads(environ["wsgi.input"].read(int(environ["CONTENT_LENGTH"])))
except (KeyError, IOError, ValueError):
return common.show_error("Error while parsing JSON data.", start_response)
# Make sure the submitted data was contained within an object at least
if not isinstance(data, dict):
return common.show_error("Error, data must be contained within an object.", start_response)
# Log the data to a file
log_dir = config.get("filterhitstats", "log_dir")
try:
log_file = log_filterhits(data, log_dir, environ.get("QUERY_STRING", ""))
except (OSError, IOError):
traceback.print_exc()
return common.show_error("Failed to write data to log file!", start_response,
"500 Logging error")
# Update the geometrical_mean aggregations in the database
interval = config.get("filterhitstats", "interval")
try:
db_connection = db.connect()
try:
db.write(db_connection, geometrical_mean.update(interval, data))
finally:
db_connection.close()
except:
# Updating the aggregations in the database failed for whatever reason,
# log the details but continue to return 204 to the client to avoid the
# re-transmission of data.
processing_error_log = os.path.join(config.get("filterhitstats", "log_dir"),
"processing-errors.log")
with open(processing_error_log, "a+") as f:
message = "Problem processing data file %s:\n%s" % (
log_file, traceback.format_exc()
)
print >> f, "[%s] %s" % (datetime.now().strftime("%d/%b/%Y:%H:%M:%S %z"), message)
# Send back a 204 No Content
start_response("204 No Content", [])
return []
示例12: handleRequest
def handleRequest(environ, start_response):
setupStderr(environ['wsgi.errors'])
params = parse_qs(environ.get('QUERY_STRING', ''))
id = params.get('id', [''])[0].lower()
if not re.match(r'^[\da-f]{32}$', id):
return showError('Invalid or missing ID', start_response)
user = getUser(id)
if user == None:
return showError('User not found', start_response)
user['reportlist'] = getReportsForUser(id)
template = get_template(get_config().get('reports', 'showUserTemplate'))
start_response('200 OK', [('Content-Type', 'application/xhtml+xml; charset=utf-8')])
return [template.render(user).encode('utf-8')]
示例13: handleSubscriptionFallbackRequest
def handleSubscriptionFallbackRequest(environ, start_response):
setupStderr(environ['wsgi.errors'])
(redirects, gone)= getData()
start_response('200 OK', [('Content-Type', 'text/plain')])
url = None
params = parse_qs(environ.get('QUERY_STRING', ''))
if 'url' in params:
url = params['url'][0]
if url and url in gone:
return ['410']
elif url and url in redirects:
return ['301 %s' % redirects[url]]
return []
示例14: main
def main():
"""
main function for createNightlies.py
"""
setupStderr()
nightlyConfig = ConfigParser.SafeConfigParser()
nightlyConfigFile = get_config().get('extensions', 'nightliesData')
if os.path.exists(nightlyConfigFile):
nightlyConfig.read(nightlyConfigFile)
# build all extensions specified in the configuration file
# and generate changelogs and documentations for each:
data = None
for repo in Configuration.getRepositoryConfigurations(nightlyConfig):
build = None
try:
build = NightlyBuild(repo)
if build.hasChanges():
build.run()
except Exception, ex:
print >>sys.stderr, "The build for %s failed:" % repo
traceback.print_exc()
示例15: handleRequest
def handleRequest(environ, start_response):
setupStderr(environ['wsgi.errors'])
if not environ.get('HTTP_X_ADBLOCK_PLUS'):
return showError('Please use Adblock Plus to submit reports', start_response)
if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYPE', '').startswith('text/xml'):
return showError('Unsupported request method', start_response)
params = parse_qs(environ.get('QUERY_STRING', ''))
requestVersion = params.get('version', ['0'])[0]
if requestVersion != '1':
return showError('Unsupported request version', start_response)
guid = params.get('guid', [''])[0].lower()
if not re.match(r'^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$', guid):
return showError('Invalid or missing GUID', start_response)
path = os.path.join(get_config().get('reports', 'dataPath'), guid + '.xml')
if os.path.exists(path) or os.path.exists(path + '.tmp'):
return showError('Duplicate GUID', start_response)
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
try:
file = open(path + '.tmp', 'wb')
iter = dataIterator(environ['wsgi.input'], file)
knownIssues = knownIssuesParser.findMatches(iter, params.get('lang', ['en-US'])[0])
file.close()
os.rename(path + '.tmp', path);
except Exception, e:
if os.path.isfile(path + '.tmp'):
os.remove(path + '.tmp')
raise e