小编Tyl*_*ell的帖子

Django,Nginx和Gunicorn上的SSL

我正在使用Nginx和gunicorn服务我的Django项目。我目前有它适用于ssl(https),但不太了解设置文件和nginx的正确设置。有人可以看看我的设置,并告诉我是否有任何公然看起来错误或可怕地执行的事情?

我的Nginx文件,请注意,有些行已被注释掉。当我取消评论时,该站点停止工作。编辑:当我同时取消所有注释/

server {

    server_name example.com;
    listen 443 ssl;
    ssl on;

    ssl_certificate /etc/ssl/mycrt.crt;
    ssl_certificate_key /etc/ssl/mykey.key;

    location = /favicon.ico {access_log off; log_not_found off;}
    location /static/ {
          gzip on;
          gzip_types text/css;
          alias /home/project/static/;
   }

   location / {
      include proxy_params;
    # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header    X-Forwarded-Proto https;
    # proxy_set_header Host $http_host;
    # proxy_redirect off;
      proxy_pass http://unix:/home/project/myproject/project.sock;
 }
}
server {
        listen 80;
        server_name example.com www.example.com;
        return 301 https://example.com$request_uri;

}
Run Code Online (Sandbox Code Playgroud)

我的独角兽文件

[Unit]
Description=gunicorn daemon
After= network.target

[Service]
User=tyler
Group=www-data
Environment="Production=production"
WorkingDirectory=/home/project/myproject
ExecStart=/home/project/projectenv/bin/gunicorn --access-logfile - …
Run Code Online (Sandbox Code Playgroud)

django ssl https nginx gunicorn

5
推荐指数
1
解决办法
809
查看次数

GitLab CI Django和Postgres

我在让GitLab的CI与Django项目一起工作时遇到问题。我使用的是Postgres服务器,看来我设置有误。任何帮助将非常感激!

错误:

django.db.utils.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)

我的.gitlab-ci.yml文件:

image: python:3.6.1

services:
    - postgres:9.3

variables:
  POSTGRES_DB: project_ci_test
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: ""

test:
    script:
    - export DATABASE_URL=postgres://postgres:@postgres:5432/project_ci_test
    - apt-get update -qy
    - apt-get install -y python-dev python-pip
    - pip install -r requirements.txt
    - python manage.py makemigrations
    - python manage.py migrate
    - python manage.py test Project
Run Code Online (Sandbox Code Playgroud)

在我的Django设置文件中:

DATABASES = {
    'default': {
        'ENGINE' : …
Run Code Online (Sandbox Code Playgroud)

django postgresql gitlab gitlab-ci gitlab-ci-runner

5
推荐指数
1
解决办法
1650
查看次数

Python-Docx 将 HTML 插入 Docx

是否可以使用应用了样式的 python-docx 将 HTML 插入到文档中?我唯一需要做的就是斜体。

例如,如何插入"Today is <i>Saturday</i>."星期六实际上是用斜体插入的?

谢谢!

html python italics python-docx

4
推荐指数
2
解决办法
3903
查看次数

Django和Nginx X-accel-redirect

到目前为止,我一直在摸索尝试保护Django的媒体文件而没有运气!我只是想让只有管理员用户才能访问媒体文件夹的地方。这是我的Nginx文件。

server {
    listen 80;
    server_name xxxxxxxxxx;

    location = /favicon.ico {access_log off; log_not_found off;}
    location /static/ {
          alias /home/{site-name}/static_cdn/;
   }
   location /media/ {
          internal;
          root /home/{site-name}/;
   }

   location / {
this is setup and working. Didn't include Code though

}
Run Code Online (Sandbox Code Playgroud)

我的网址文件

urlpatterns = [
    url(r'^media/', views.protectedMedia, name="protect_media"),
] 
Run Code Online (Sandbox Code Playgroud)

我的看法

def protectedMedia(request):

    if request.user.is_staff:
        response = HttpResponse()
        response['Content-Type'] = ''
        response['X-Accel-Redirect'] = request.path
        return response

    else:
        return HttpResponse(status=400)
Run Code Online (Sandbox Code Playgroud)

这将产生404 Not Found Nginx错误。这里有什么公然的错误吗?谢谢!

顺便说一句,我尝试在Nginx设置的根URL末尾添加/ media /。

django ubuntu nginx sendfile django-media

3
推荐指数
2
解决办法
1855
查看次数

Python删除所有文件夹但不删除文件

我想写一段 python 代码来删除所有文件夹及其内容,但不删除单个文件。
例如,这里有一些包含在目录(文件夹 B)中的文件和文件夹以及执行删除的脚本文件。如何删除文件夹A、文件夹B、文件夹C等,但保留文件?谢谢

/Folder B 
    file.docx
    fileB.docx
    fileC.docx
    pythonDeleteScript.py
    folderA/
    folderB/
    folderC/
    folderD/
Run Code Online (Sandbox Code Playgroud)

python shutil

2
推荐指数
1
解决办法
1443
查看次数

Python Beautiful Soup 提取 HTML 元数据

我得到了一些我不太明白的奇怪行为。我希望有人可以解释发生了什么。

考虑这个元数据:

<meta property="og:title" content="This is the Tesla Semi truck">
<meta name="twitter:title" content="This is the Tesla Semi truck">
Run Code Online (Sandbox Code Playgroud)

此行成功找到所有“og”属性并返回一个列表。

opengraphs = doc.html.head.findAll(property=re.compile(r'^og'))
Run Code Online (Sandbox Code Playgroud)

然而,这条线不能为 twitter 卡做同样的事情。

twitterCards = doc.html.head.findAll(name=re.compile(r'^twitter'))
Run Code Online (Sandbox Code Playgroud)

为什么第一行成功找到了所有的“og”(opengraph卡),却找不到推特卡?

html python twitter beautifulsoup web-scraping

2
推荐指数
1
解决办法
1087
查看次数