Temporarily Redirect stdout/stderr

You can also put the redirection logic in a contextmanager. import os import sys class RedirectStdStreams(object): def __init__(self, stdout=None, stderr=None): self._stdout = stdout or sys.stdout self._stderr = stderr or sys.stderr def __enter__(self): self.old_stdout, self.old_stderr = sys.stdout, sys.stderr self.old_stdout.flush(); self.old_stderr.flush() sys.stdout, sys.stderr = self._stdout, self._stderr def __exit__(self, exc_type, exc_value, traceback): self._stdout.flush(); self._stderr.flush() sys.stdout = self.old_stdout sys.stderr … Read more

Rerouting stdin and stdout from C

Why use freopen()? The C89 specification has the answer in one of the endnotes for the section on <stdio.h>: 116. The primary use of the freopen function is to change the file associated with a standard text stream (stderr, stdin, or stdout), as those identifiers need not be modifiable lvalues to which the value returned … Read more

jQuery and AJAX response header

cballou’s solution will work if you are using an old version of jquery. In newer versions you can also try: $.ajax({ type: ‘POST’, url:’url.do’, data: formData, success: function(data, textStatus, request){ alert(request.getResponseHeader(‘some_header’)); }, error: function (request, textStatus, errorThrown) { alert(request.getResponseHeader(‘some_header’)); } }); According to docs the XMLHttpRequest object is available as of jQuery 1.4.

Nginx no-www to www and www to no-www

HTTP Solution From the documentation, “the right way is to define a separate server for example.org”: server { listen 80; server_name example.com; return 301 http://www.example.com$request_uri; } server { listen 80; server_name www.example.com; … } HTTPS Solution For those who want a solution including https://… server { listen 80; server_name www.domain.com; # $scheme will get the … Read more