标签 python 下的文章

第一种方法:

rds = redis.StrictRedis(host='localhost', port=6379, password='123456')

第二种方法:

redis_config = {
        'host': os.getenv('REDIS_HOST', '127.0.0.1'),
        'port': os.getenv('REDIS_PORT', 6379),
        'password': os.getenv('REDIS_PWD', '123456')
    }
redis_uri = 'redis://:{password}@{host}:{port}/0'.format(**redis_config)
redis = Redis.from_url(redis_uri)

In the Django's tutorial, I created an app name front, and its urls.py definition like this:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('front/', include('front.urls')),
    path('admin/', admin.site.urls),
]

So, if the app's url in web browser is http://127.0.0.1:8000/front/
In fact, most website the home page won't have subdirectory. So, the solutions is here:

from django.contrib import admin
from django.urls import path, include, re_path

urlpatterns = [
    re_path(r'^', include('front.urls')),
    path('front/', include('front.urls')),
    path('admin/', admin.site.urls),
]

Now, you can visit the app root url in the home page. http://127.0.0.1:8000/

class RegexConverter(BaseConverter):
    def __init__(self, map, *args):
        BaseConverter.__init__(self, map)
        self.map = map
        self.regex = args[0]


app.url_map.converters['regex'] = RegexConverter

Define a regex route

@app.route('/go/<regex("[a-zA-Z0-9]+"):url>')
def go(url):
    real_url = redis.get(url)
    return redirect(real_url)

Dockerfile使用的python3.7作为基础镜像 FROM python:3.7,但是执行
RUN pip install --no-cache-dir -r requirements.txt
是默认走了这个源,https://files.pythonhosted.org/packages,编译用时太久。

无赖修改成国内源来加快编译速度:
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir -r requirements.txt

附国内其它源:
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/