cURL 사용 시 데이터(text/plain, x-www-form-urlencoded 또는 json)를 같이 보내는 법을 알아보자.


환경

  • Linux
  • cURL


cURL

옵션을 사용해 데이터를 포함한 요청 보내기

  • -d 옵션을 사용
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'


사용 예제

text/plain

  • -d 옵션을 사용하고 POST 방식으로 text 형태의 자료를 보내는 예제
curl -XPOST -H "Content-Type: text/plain" -d "raw text data" http://127.0.0.1:5000/test

application/x-www-form-urlencoded

  • -d 옵션을 사용하고 POST 방식으로 x-www-form-urlencoded 형태의 자료를 보내는 예제
curl -XPOST -H "Content-Type: application/x-www-form-urlencoded" -d "param1=value1&param2=value2" http://127.0.0.1:5000/test

application/json

  • -d 옵션을 사용하고 POST 방식으로 JSON 형태의 자료를 보내는 예제
curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:5000/test -d '
{
   "test": "1234test",
   "child": {
      "child_1": "Hi",
      "cihld_2": "Hi"
   },
   "elements": [ "a", "b", "c" ]
}
'


참고자료

Summarize how to attach data(text/plain, x-www-form-urlencoded or json) when using cURL


Environment and Prerequisite

  • Linux
  • cURL


cURL

Send request with data using option

  • Use -d option.
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'


Usage Example

text/plain

  • Example of sending text format data with using -d option and POST method.
curl -XPOST -H "Content-Type: text/plain" -d "raw text data" http://127.0.0.1:5000/test

application/x-www-form-urlencoded

  • Example of sending x-www-form-urlencoded format data with using -d option and POST method.
curl -XPOST -H "Content-Type: application/x-www-form-urlencoded" -d "param1=value1&param2=value2" http://127.0.0.1:5000/test

application/json

  • Example of sending JSON format data with using -d option and POST method.
curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:5000/test -d '
{
   "test": "1234test",
   "child": {
      "child_1": "Hi",
      "cihld_2": "Hi"
   },
   "elements": [ "a", "b", "c" ]
}
'


Reference

ssh 접속시 host의 key를 확인하지 않고 접속하는 방법을 알아보자.


환경

  • Linux
  • SSH(OpenSSH)


Host Key Checking

  • ssh를 이용해 원격에 있는 서버에 접근할 때 해당 서버에 대한 인증을 위해 로컬에 저장해둔 키와 서버의 키를 비교하는 작업을 진행한다.
  • 이런 작업을 통해 중간자 공격 같은 부분을 예방할 수 있다.


SSH 접속시 Host Key Checking 비활성화

  • 해당 옵션을 사용해도 만약 저장된 서버의 키가 없다면 ~/.ssh/known_hosts에 추가합니다.
  • 저장된 키가 서버의 키와 다르더라도 접속을 진행합니다.


1. ssh 명령어 사용 시 비활성화

  • StrictHostKeyChecking=no 옵션을 이용
ssh -o StrictHostKeyChecking=no [DOMAIN_OR_IP]


2. ssh config 파일을 수정

~/.ssh/config

  • 아래처럼 특정 호스트에 대해 옵션을 추가할 수 있다.
Host twpower-private-server
    HostName [IP ADDRESS]
    StrictHostKeyChecking no
    Port 22
    User [USERNAME]
    IdentityFile [IDENTITY KEY FILE]

/etc/ssh/ssh_config

  • 아래처럼 StrictHostKeyChecking no를 추가하며 *가 있기 때문에 접속하는 모든 호스트에 대해 Key 확인을 하지 않습니다.
  • /etc/ssh/ssh_config 파일은 해당 시스템에 있는 모든 유저에게 적용되는 옵션을 담고 있습니다.
Host *
    StrictHostKeyChecking no


참고자료

Summarize methods to disable host key checking while connecting ssh.


Environment and Prerequisite

  • Linux
  • SSH(OpenSSH)


Host Key Checking

  • When we connect to remote server by using ssh, client compares client’s saved key and remote server’s key to authenticate server.
  • This process prevents from Man-in-the-middle attack.


Disable Host Key Checking When Using SSH

  • Remote host’s key will be added if there is no added key in ~/.ssh/known_hosts.
  • Connect to remote server even though saved key is different from server’s key.


1. Disable when using ssh command

  • Use StrictHostKeyChecking=no option.
ssh -o StrictHostKeyChecking=no [DOMAIN_OR_IP]


2. Edit ssh config file

~/.ssh/config

  • Add option to specific host like below.
Host twpower-private-server
    HostName [IP ADDRESS]
    StrictHostKeyChecking no
    Port 22
    User [USERNAME]
    IdentityFile [IDENTITY KEY FILE]

/etc/ssh/ssh_config

  • There is * in config file. So this disables host key checking to all hosts.
  • /etc/ssh/ssh_config defines all the default settings for the client utilities for all users on that system.
Host *
    StrictHostKeyChecking no


Reference

파이썬(Python) List Comprehensions에 대해 알아보자.


환경

  • Linux
  • Python 3.X.X


파이썬(Python) List Comprehensions

List Comprehensions

results=[]
for i in range(10):
    results.append(i*2)
results=[x*2 for x in range(10)]
  • List Comprehensions: 파이썬에서 리스트 자료구조를 조금 더 간결하게 만들 수 있도록 도와주는 기능입니다.
  • 리스트를 새로 생성할 때 각각의 원소가 어떠한 연산의 결과이거나 조건을 만족하는 부분집합을 만들 때 사용할 수 있습니다.


형태

list_name = [element_format for clause]
  • [ ]로 감싸고 제일 앞에 추가할 원소의 형태를 넣으며 그 다음에 for문을 넣습니다.
  • 아래에 나오지만 for문 중첩도 가능합니다.


예제

제곱

results=[]
for i in range(10):
    results.append(i**2)
results=[x**2 for x in range(10)]

튜플 만들기

results=[]
for x in [1,3,5]:
    for y in [2,4,6]:
        results.append((x,y))
results=[(x,y) for x in [1,3,5] for y in [2,4,6]]

함수와 함께 사용하기

from math import pi

results=[]
for i in range(1, 6):
    results.append(str(round(pi, i)))
from math import pi
results=[str(round(pi, i)) for i in range(1, 6)]

중첩

results=[]
for x in [1,2]:
    for y in [3,4]:
        for z in [5,6]:
            results.append((x,y,z))
results=[(x,y,z) for x in [1,2] for y in [3,4] for z in [5,6]]

조건문과 함께 사용하기

results=[]
for x in range(20):
    if x%2 == 0:
        results.append(x)
results=[x for x in range(20) if x%2 == 0]


참고자료