Skip to content

Instantly share code, notes, and snippets.

@ytaminE
Created December 10, 2017 22:09
Show Gist options
  • Select an option

  • Save ytaminE/523c53c5c73087e38879ba25e31120c1 to your computer and use it in GitHub Desktop.

Select an option

Save ytaminE/523c53c5c73087e38879ba25e31120c1 to your computer and use it in GitHub Desktop.
import urllib2
import StringIO
import sys
import contextlib
import ast
@contextlib.contextmanager
def stdoutIO(stdout=None):
old = sys.stdout
if stdout is None:
stdout = StringIO.StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
def lambda_handler(event, context):
name = event['name']
response = urllib2.urlopen(event['url'])
input = event['input']
input = ast.literal_eval(input)
code = response.read()
with stdoutIO() as s:
try:
exec(code)
result = locals()[name](input)
print result
except Exception as err:
return "Unexpected error:" + str(err)
return s.getvalue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment