12.cors.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import signal
  2. import subprocess
  3. import time
  4. from selenium.webdriver import Chrome
  5. import template
  6. import util
  7. from pywebio.input import *
  8. from pywebio.utils import run_as_function
  9. def target():
  10. template.basic_output()
  11. template.background_output()
  12. run_as_function(template.basic_input())
  13. actions(buttons=['Continue'])
  14. template.background_input()
  15. def test_once(browser: Chrome, output_file):
  16. template.test_output(browser)
  17. template.test_input(browser)
  18. time.sleep(1)
  19. template.save_output(browser, output_file)
  20. def test(server_proc: subprocess.Popen, browser: Chrome):
  21. test_once(browser, '12.tornado_cors.html')
  22. server_proc.send_signal(signal.SIGINT)
  23. time.sleep(4)
  24. browser.get('http://localhost:5000/?pywebio_api=http://localhost:8081/io')
  25. test_once(browser, '12.aiohttp_cors.html')
  26. server_proc.send_signal(signal.SIGINT)
  27. time.sleep(4)
  28. browser.get('http://localhost:5000/?pywebio_api=http://localhost:8082/io')
  29. test_once(browser, '12.flask_cors.html')
  30. def start_test_server():
  31. from pywebio import start_server as tornado_server
  32. from pywebio.platform.flask import start_server as flask_server
  33. from pywebio.platform.aiohttp import start_server as aiohttp_server
  34. import asyncio
  35. util.start_static_server()
  36. try:
  37. tornado_server(target, port=8080, allowed_origins=['http://localhost:5000'])
  38. except:
  39. print('tornado_server exit')
  40. loop = asyncio.new_event_loop()
  41. asyncio.set_event_loop(loop)
  42. try:
  43. aiohttp_server(target, port=8081, allowed_origins=['http://localhost:5000'])
  44. except:
  45. print('aiohttp_server exit')
  46. try:
  47. flask_server(target, port=8082, allowed_origins=['http://localhost:5000'])
  48. except:
  49. print('flask_server exit')
  50. if __name__ == '__main__':
  51. util.run_test(start_test_server, test,
  52. address='http://localhost:5000/?pywebio_api=http://localhost:8080/io')