PYTHON
-
[Python] Linux 디렉토리와 파일 리포팅Python/Python for Linux 2019. 3. 27. 11:29
python의 os.walk module을 이용하면 디렉토리와 파일정보를 출력할 수 있고, os.path module을 추가하면 파일사이즈, 생성일, 수정일, 변경일 등을 추가로 활용할 수 있습니다. 디렉토리는 샘플이기 때문에 테썹이기 때문에 /etc로 하였는데, 실썹은 조심해야죠. 경로는 input으로 받아도 되겠습니다. python version : 2.7 [ Sample 1 - 특정 디렉토리내에 있는 파일명과 디렉토리 경로 보기 ] #!/usr/bin/python import os sum = 0 for (path, dir, files) in os.walk('/etc'): for filename in files: splitfilename = filename.split('.') print path, f..
-
파이썬(Python) 설치Python/Python Programming 2019. 3. 17. 10:02
파이썬(python) 다운로드 : https://www.python.org - 운영체제 플랫폼에 맞게 다운로드. (현재는 3.7.2)- Windows XP와 그 이전버전의 운영체제는 3.5 이상버전을 사용할 수 없음 - Install Now : 기본설치- Customize installation : 사용자 설치 - Add_Python 3.7 PATH : PATH 환경변수가 필요없다면 체크 않해도 된다. 필자는 환경변수를 체크하고, 사용자 설치로 진행 - 디렉토리는 별도로 생성- Debug 옵션 필요시 체크 파이썬(Python) 3.7.2가 정상적으로 설치되었다.
-
python, mariadb, Grafana를 이용한 URL 모니터링 (1) - 구축System ManageMent 2018. 12. 30. 02:41
- Python : 웹 사이트 URL Check - mariadb : check 결과를 DB화. (Grafana에서 지원하는 Data Source 중 선택) - Grafana : DashBoard 1. python으로 웹 사이트 핼스체크 import requests import time import pymysql from datetime import datetime conn = pymysql.connect(host=' ', user=' ', password=' ', db=' ', charset='utf8') c = conn.cursor() url = ('url1','url2') # urllist, http프로토콜까지 붙임 def func1(): sql = 'insert into urlcheck (logd..
-
[Python] SQLite3 distinct 중복제거Python/SQLite 2018. 8. 15. 21:13
Table alphabet value E 2 B 2 C 4 D 1 A 3 E 4 A 1 D 3 C 2 B 1 alphabet 컬럼 중복 제거 c = conn.cursor() rows = c.execute('''select DISTINCT alphabet from test''') for row in rows: print(row) ----------------------------------------------------------- ('E',) ('B',) ('C',) ('D',) ('A',) value 컬럼 중복 제거 c = conn.cursor() rows = c.execute('''select DISTINCT value from test''') for row in rows: print(row) --..
-
[Python] FTP TLS Download 다운로드Python/Python Programming 2018. 8. 7. 01:44
DOCS : https://docs.python.org/3.6/library/ftplib.html?highlight=ftp_tls from ftplib import FTP_TLS ftps = FTP_TLS('host') ftps.login('host', 'passwd') ftps.prot_p() filename = 'test1.txt' myfile = open(filename, 'wb') ftps.retrbinary('RETR %s' % filename, myfile.write) ftps.close()
-
[Python] Elasticsearch MonitoringElasticsearch 2018. 7. 22. 22:56
파이썬을 이용하여 Elasticsearch에 index 생성하고, 삭제하는 방법으로 모니터링 try: es = Elasticsearch(hosts=' ', port=' ') doc = ''' { "mappings": { "testindex": { "properties": { "msg": { "type": "keyword" } } } } }''' res = es.indices.create(index='testindex', body=doc) print('index create : %s' %res['acknowledged']) res = es.indices.delete(index='testindex') print('index delete : %s' %res['acknowledged']) except Excep..
-
[Python] 디렉토리(하위포함) 파일명 점검 하기Python/Python for Windows 2018. 7. 9. 01:53
웹 서버를 운영하다 보면 웹 소스, 동영상 등 파일명을 고려해야 하는데, 시스템 명령어나 SQL쿼리문, 특문과 같은 파일명 들어가 있는 파일명은 보안장비에서 오탐으로 필터가 될 수 있다. 파이썬으로 특정폴더 이하에 있는 파일을 간단하게 점검할 수 있다. 파일명에 SQL 쿼리 명령어가 포함된 파일명 체크 sysCmd = {'select':'slct', 'update':'updt','insert':'inst'} for (path, dir, files) in os.walk(r'D:\test4'): for filename in files: for i in sysCmd: if i in filename: print('%s 파일에 %s 가 포함되어 있습니다. %s 로 변경을 권장드립니다.' %(filename,i,s..
-
[Python] Comparison of Datetime Type Using PythonPython/Python Programming 2018. 6. 15. 18:33
파이썬의 datetime 타입끼리 비교 연산이 가능하며, 결과는 True, False 로 리턴 from datetime import datetime from datetime import timedelta today = datetime.today() yesterday = today - timedelta(days=1) compare = today > yesterday print(compare) True