브 라 우 저 에서 Python 코드 를 직접 실행 합 니 다.

13655 단어 Python/Rubypython
이 링크 에 코드 를 로 컬 에 다운로드 한 다음 cmd 를 열 고 python 으로 이 파일 을 실행 한 다음 창 을 닫 지 마 십시오https://raw.githubusercontent.com/michaelliao/learn-python3/master/teach/learning.pycmd 조작
E:\python\code>python learning.py
Ready for Python code on port 39093...
127.0.0.1 - - [29/Nov/2016 14:51:17] "GET / HTTP/1.1" 200 216
127.0.0.1 - - [29/Nov/2016 14:51:17] "GET /favicon.ico HTTP/1.1" 400 23

이렇게 해서 39093 포트 를 열 고 브 라 우 저 에 직접 접근 할 수 있 습 니 다.
http://127.0.0.1:39093/

하지만 이렇게 코드 를 제출 할 때 bad request 를 알려 줍 니 다.
그래서 hosts 파일 을 설정 해 야 합 니 다.
127.0.0.1      local.liaoxuefeng.com

다음 방식 으로 접근:
http://local.liaoxuefeng.com:39093/

백업 하 다
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

r'''
learning.py

A Python 3 tutorial from http://www.liaoxuefeng.com

Usage:

python3 learning.py
'''

import sys

def check_version():
    v = sys.version_info
    if v.major == 3 and v.minor >= 4:
        return True
    print('Your current python is %d.%d. Please use Python 3.4.' % (v.major, v.minor))
    return False

if not check_version():
    exit(1)

import os, io, json, subprocess, tempfile
from urllib import parse
from wsgiref.simple_server import make_server

EXEC = sys.executable
PORT = 39093
HOST = 'local.liaoxuefeng.com:%d' % PORT
TEMP = tempfile.mkdtemp(suffix='_py', prefix='learn_python_')
INDEX = 0

def main():
    httpd = make_server('127.0.0.1', PORT, application)
    print('Ready for Python code on port %d...' % PORT)
    httpd.serve_forever()

def get_name():
    global INDEX
    INDEX = INDEX + 1
    return 'test_%d' % INDEX

def write_py(name, code):
    fpath = os.path.join(TEMP, '%s.py' % name)
    with open(fpath, 'w', encoding='utf-8') as f:
        f.write(code)
    print('Code wrote to: %s' % fpath)
    return fpath

def decode(s):
    try:
        return s.decode('utf-8')
    except UnicodeDecodeError:
        return s.decode('gbk')

def application(environ, start_response):
    host = environ.get('HTTP_HOST')
    method = environ.get('REQUEST_METHOD')
    path = environ.get('PATH_INFO')
    if method == 'GET' and path == '/':
        start_response('200 OK', [('Content-Type', 'text/html')])
        return [b'Learning Python

좋은 웹페이지 즐겨찾기