CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

44 lines
1.5 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2020 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import json
  17. import urllib.request, urllib.parse, urllib.error
  18. SANDBOX_URL = "https://catsoop.mit.edu/python_sandbox_v2019.9/"
  19. def run_code(
  20. context,
  21. code,
  22. options,
  23. count_opcodes=False,
  24. opcode_limit=None,
  25. result_as_string=False,
  26. ):
  27. code = code.replace("\r\n", "\n")
  28. data = urllib.parse.urlencode(
  29. {"code": code, "result_as_string": result_as_string}
  30. ).encode()
  31. request = urllib.request.Request(context.get("csq_sandbox_url", SANDBOX_URL), data)
  32. try:
  33. resp = urllib.request.urlopen(request, data).read()
  34. return json.loads(resp.decode())
  35. except:
  36. err = "CAT-SOOP: Could not connect to %s" % context.get(
  37. "csq_sandbox_url", SANDBOX_URL
  38. )
  39. return {"fname": "", "out": "", "err": err, "info": {}}