使用python

时间:2017-03-11 17:33:11

标签: python python-2.7

我想在我的Nginx-config文件中查找特定文本(" dev-example"),并使用python脚本(python 2.7)删除相关的服务器块。

我的部分文件如下所示:

server {
       listen      80;
       server_name     dev-example.com;
       location / {
          proxy_pass         http://dev-example.io:5015/;
          proxy_redirect     off;

          ##proxy_set_header   Host             $host;
          proxy_set_header   X-Real-IP        $remote_addr;
          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

          client_max_body_size       10m;
          client_body_buffer_size    128k;

          proxy_connect_timeout      90;
          proxy_send_timeout         90;
          proxy_read_timeout         90;

          proxy_buffer_size          4k;
          proxy_buffers              4 32k;
          proxy_busy_buffers_size    64k;
          proxy_temp_file_write_size 64k;
       }
}

server {
       listen      80;
       server_name     test.example.com;
       location / {
          proxy_pass         http://test.example.io:5016/;
          proxy_redirect     off;

          ##proxy_set_header   Host             $host;
          proxy_set_header   X-Real-IP        $remote_addr;
          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

          client_max_body_size       10m;
          client_body_buffer_size    128k;

          proxy_connect_timeout      90;
          proxy_send_timeout         90;
          proxy_read_timeout         90;

          proxy_buffer_size          4k;
          proxy_buffers              4 32k;
          proxy_busy_buffers_size    64k;
          proxy_temp_file_write_size 64k;
       }
}

输出应如下:

server {
       listen      80;
       server_name     test.example.com;
       location / {
          proxy_pass         http://test.example.io:5016/;
          proxy_redirect     off;

          ##proxy_set_header   Host             $host;
          proxy_set_header   X-Real-IP        $remote_addr;
          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

          client_max_body_size       10m;
          client_body_buffer_size    128k;

          proxy_connect_timeout      90;
          proxy_send_timeout         90;
          proxy_read_timeout         90;

          proxy_buffer_size          4k;
          proxy_buffers              4 32k;
          proxy_busy_buffers_size    64k;
          proxy_temp_file_write_size 64k;
       }
}

我不想为输出创建新文件,我只想从现有文件中删除这些行。据我所知,我可以使用正则表达式:

def remove(text)
   with open('nginx.conf') as f:
      for word in f:
         word = word.strip()
         if re.search(text,word):
            rx = re.compile("""server.*?{.*?server_name\s*text.*?}""", re.VERBOSE)
            re.sub(rx, '', ...)

remove("dev-example")

但我不知道如何删除它们。我不知道我可以在re.sub()的第三个艺术中加入什么,此外,当有内部'时,模式不起作用。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

好的,这种方法非常简单。它没有使用正则表达式,但它可以适用于你的情况。

def remove_block_from_file(filename, search_text):
    output = ""
    with open(filename,'r') as f:
        output = f.read()

    blocks = [i for i in output.split("server {") if i != ""] # Removes the empty entries in the `block` list.

    output = ""
    for b in blocks:
        if search_text not in b: # If the text isn't found in that block, append it to the new output.
            output += "server {" + b + "\n"


    with open(filename, 'w') as f:
        f.write(output)

所以在你的情况下这样称呼它:

remove_block_from_file("nginx.conf", "dev-example")

这将搜索该块中的任何文本,因为这是您在问题中提出的问题,而不是专门针对服务器名称。

我希望这会有所帮助。

答案 1 :(得分:1)

整个regex版本。

import re
def remove(text):
  with open('nginx.conf', 'r') as f:
    word = f.read()
    if re.search(text, word):
      rx = re.compile('server \{.*server_name\s*' + text + '.*?^}', re.DOTALL | re.MULTILINE)
      word = re.sub(rx, '', word)
      with open('nginx.conf', 'w') as f:
        f.write(word)

remove("dev-example")

基本上,它搜索server { ... server_name ... dev-example ... \n}。因此整个区块将被替换。 ^表示MULTILINE mode中每一行的开头,因此它会绕过缩进的内部}