14.django_multiple_session_impliment.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import subprocess
  2. import time
  3. from selenium.webdriver import Chrome
  4. import pywebio
  5. import template
  6. import util
  7. from pywebio.input import *
  8. from pywebio.utils import to_coroutine, 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. async def async_target():
  16. template.basic_output()
  17. await template.coro_background_output()
  18. await to_coroutine(template.basic_input())
  19. await actions(buttons=['Continue'])
  20. await template.coro_background_input()
  21. def test(server_proc: subprocess.Popen, browser: Chrome):
  22. template.test_output(browser)
  23. time.sleep(1)
  24. template.test_input(browser)
  25. time.sleep(1)
  26. template.save_output(browser, '14.flask_multiple_session_impliment_p1.html')
  27. browser.get('http://localhost:8080/app2?_pywebio_debug=1&_pywebio_http_pull_interval=400')
  28. template.test_output(browser)
  29. time.sleep(1)
  30. template.test_input(browser)
  31. time.sleep(1)
  32. template.save_output(browser, '14.flask_multiple_session_impliment_p2.html')
  33. urlpatterns = []
  34. def start_test_server():
  35. global urlpatterns
  36. pywebio.enable_debug()
  37. from functools import partial
  38. from pywebio.platform.django import webio_view
  39. from django.conf import settings
  40. from django.core.wsgi import get_wsgi_application
  41. from django.urls import path
  42. from django.utils.crypto import get_random_string
  43. from django.views.static import serve
  44. from pywebio import STATIC_PATH
  45. django_options = dict(
  46. DEBUG=True,
  47. ALLOWED_HOSTS=["*"], # Disable host header validation
  48. ROOT_URLCONF=__name__, # Make this module the urlconf
  49. SECRET_KEY=get_random_string(10), # We aren't using any security features but Django requires this setting
  50. )
  51. django_options.setdefault('LOGGING', {
  52. 'version': 1,
  53. 'disable_existing_loggers': False,
  54. 'formatters': {
  55. 'simple': {
  56. 'format': '[%(asctime)s] %(message)s'
  57. },
  58. },
  59. 'handlers': {
  60. 'console': {
  61. 'class': 'logging.StreamHandler',
  62. 'formatter': 'simple'
  63. },
  64. },
  65. 'loggers': {
  66. 'django.server': {
  67. 'level': 'INFO',
  68. 'handlers': ['console'],
  69. },
  70. },
  71. })
  72. settings.configure(**django_options)
  73. urlpatterns = [
  74. path(r"app", webio_view(target, cdn=False)),
  75. path(r"app2", webio_view(async_target, cdn=False)),
  76. path(r'', partial(serve, path='index.html'), {'document_root': STATIC_PATH}),
  77. path(r'<path:path>', serve, {'document_root': STATIC_PATH}),
  78. ]
  79. app = get_wsgi_application() # load app
  80. import tornado.wsgi
  81. container = tornado.wsgi.WSGIContainer(app)
  82. http_server = tornado.httpserver.HTTPServer(container)
  83. http_server.listen(8080, address='127.0.0.1')
  84. tornado.ioloop.IOLoop.current().start()
  85. if __name__ == '__main__':
  86. util.run_test(start_test_server, test,
  87. address='http://localhost:8080/app?_pywebio_debug=1&_pywebio_http_pull_interval=400')