对Python编程语言做了基本的介绍
Make a basic introduction about python programming language.
主要内容:
对Python编程语言做了基本的介绍
Make a basic introduction about python programming language.
主要内容:
During my work, I need to parse the result from some huge log files. The result is always save at the end of the file. So I need find a way to get the required last lines to accelerate the parse speed. Here is the function I have written to achieve this requirement in python.
- def last_lines(filename, lines=1):
- '''
- return the last lines from the specify files
- '''
- with open(filename, 'rb') as fh:
- fh.seek(0, os.SEEK_END)
- if fh.tell() == 0:
- return []
- fh.seek(-2, os.SEEK_END)
- #while not BOF
- while fh.tell() > 0:
- cur_char = fh.read(1).decode()
- if cur_char == '\n':
- lines -= 1
- if lines > 0:
- fh.seek(-2, os.SEEK_CUR)
- else:
- break
- return [line.decode() for line in fh.readlines()]
Send a message to RabbitMQ
- #!/usr/bin/env python
-
- import sys
- sys.path.append("..")
-
-
- import puka
-
- client = puka.Client("amqp://localhost/")
-
- promise = client.connect()
- client.wait(promise)
-
- promise = client.queue_declare(queue='test')
- client.wait(promise)
-
- promise = client.basic_publish(exchange='', routing_key='test',
- body="Hello world!")
- client.wait(promise)
-
- print " [*] Message sent"
-
- promise = client.queue_declare(queue='test', passive=True)
- print " [*] Queue size:", client.wait(promise)['message_count']
-
- promise = client.close()
- client.wait(promise)