«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

VIDEOCUBE

Python 서버 구축하기 [CGIHTTPServer] 본문

시스템

Python 서버 구축하기 [CGIHTTPServer]

라떼청년 2017. 12. 3. 00:24



CentOS 6.3 을 설치하게 되면 기본적으로  python 

root@linux-01:/data/source/mp4> python

Python 2.6.6 (r266:84292, Jun 18 2012, 14:18:47) 

[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> 

2.6.6이 설치 되어 있다.

yum update python 을 해보자


root@linux-01:/data/source/mp4> python

Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 

[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> 

2.6.6 버전으로 빌드 20120313 으로 업뎃하였다.


2.7 혹은 3.0 을 기반으로 설치를 진행할 수 있지만 리눅스에 기본적으로 설치되어 있는 버전으로 

서버를 구성해 보려고 한다. CentOS 7 은 2.7 이다.


python 3 에서 http.server 를 이용하면 되지만, 2.6에서는 SimpleHTTPServer 를 이용해서 활용해 보자.


root@linux-01:/data> python

Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 

[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import SimpleHTTPServer

>>> import SocketServer

>>> PORT = 8000

>>> Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

>>> httpd = SocketServer.TCPServer(("", PORT), Handler)

>>> print "serving at port", PORT

serving at port 8000

>>> httpd.serve_forever()


새 창을 열고 netstat -nlp 를 해보자

root@linux-01:/root> netstat -nlp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1000/nginx          

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      911/sshd            

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      987/master          

tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      1124/python         

tcp        0      0 :::22                       :::*                        LISTEN      911/sshd            

tcp        0      0 ::1:25                      :::*                        LISTEN      987/master          

Active UNIX domain sockets (only servers)

Proto RefCnt Flags       Type       State         I-Node PID/Program name    Path

unix  2      [ ACC ]     STREAM     LISTENING     8133   1/init              @/com/ubuntu/upstart

unix  2      [ ACC ]     STREAM     LISTENING     10393  987/master          public/cleanup

unix  2      [ ACC ]     STREAM     LISTENING     10400  987/master          private/tlsmgr

unix  2      [ ACC ]     STREAM     LISTENING     10404  987/master          private/rewrite

unix  2      [ ACC ]     STREAM     LISTENING     10408  987/master          private/bounce

unix  2      [ ACC ]     STREAM     LISTENING     10412  987/master          private/defer

unix  2      [ ACC ]     STREAM     LISTENING     10416  987/master          private/trace

unix  2      [ ACC ]     STREAM     LISTENING     10420  987/master          private/verify

unix  2      [ ACC ]     STREAM     LISTENING     10424  987/master          public/flush

unix  2      [ ACC ]     STREAM     LISTENING     10428  987/master          private/proxymap

unix  2      [ ACC ]     STREAM     LISTENING     10432  987/master          private/proxywrite

unix  2      [ ACC ]     STREAM     LISTENING     10436  987/master          private/smtp

unix  2      [ ACC ]     STREAM     LISTENING     10440  987/master          private/relay

unix  2      [ ACC ]     STREAM     LISTENING     10444  987/master          public/showq

unix  2      [ ACC ]     STREAM     LISTENING     10448  987/master          private/error

unix  2      [ ACC ]     STREAM     LISTENING     10452  987/master          private/retry

unix  2      [ ACC ]     STREAM     LISTENING     10456  987/master          private/discard

unix  2      [ ACC ]     STREAM     LISTENING     10460  987/master          private/local

unix  2      [ ACC ]     STREAM     LISTENING     10464  987/master          private/virtual

unix  2      [ ACC ]     STREAM     LISTENING     10468  987/master          private/lmtp

unix  2      [ ACC ]     STREAM     LISTENING     10472  987/master          private/anvil

unix  2      [ ACC ]     STREAM     LISTENING     10476  987/master          private/scache


호출해 보자

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>

<title>Directory listing for /</title>

<body>

<h2>Directory listing for /</h2>

<hr>

<ul>

<li><a href="lost%2Bfound/">lost+found/</a>

<li><a href="source/">source/</a>

</ul>

<hr>

</body>

</html>


localhost.localdomain - - [03/Dec/2017 00:46:06] "GET / HTTP/1.1" 200 -


위와 같이 소스로 처리를 해도 되고 명령행으로도 서버를 구동시킬 수 있다.

root@linux-01:/data> python -m SimpleHTTPServer 8000 .

Serving HTTP on 0.0.0.0 port 8000 ...


위와 같이 처리를 하는 경우에는 정적 파일에 한정적으로 사용하기 위함이고, 실질적으로 

웹서버와 개발에 필요한 스크립트를 이용하여 결과물을 출력하기 위해서는 CGI 를 이용 해보도록 해야 한다.

mkdir -p /data/web/cgi-bin

cd /data/web


root@linux-01:/data/web> cat index.html 

<html>

<body>

PYTHON

</body>

</html>


root@linux-01:/data> python -m SimpleHTTPServer 8000 .

Serving HTTP on 0.0.0.0 port 8000 ...

root@linux-01:/root> curl -i "http://localhost:8000"

HTTP/1.0 200 OK

Server: SimpleHTTP/0.6 Python/2.6.6

Date: Sat, 02 Dec 2017 16:10:41 GMT

Content-type: text/html

Content-Length: 41

Last-Modified: Sat, 02 Dec 2017 16:10:33 GMT

<html>

<body>

PYTHON

</body>

</html>



Python CGI 로 구동 하기에 앞써 우선 작업 폴더를 다음과 같이 만들었다.

cd /data/web/cgi-bin

touch hello.py

vi hello.py

#!/usr/bin/env python

print "Content-type:text/html\r\n\r\n"

print '<html>'

print '<head>'

print '<title>Hello Word - First CGI Program </title>'

print '</head>'

print '<body>'

print '<h2> Hello Word! Python CGI Program</h2>'

print '</body>'

print '</html>'


SimpleHTTPServer 로 구동할 경우 :  python text 파일로 보여집니다.

python -m SimpleHTTPServer 8000 .

root@linux-01:/root> curl -i "http://localhost:8000/cgi-bin/hello.py"

HTTP/1.0 200 OK

Server: SimpleHTTP/0.6 Python/2.6.6

Date: Sat, 02 Dec 2017 16:20:38 GMT

Content-type: text/plain

Content-Length: 256

Last-Modified: Sat, 02 Dec 2017 16:15:50 GMT

#!/usr/bin/python

print "Content-type:text/html\r\n\r\n"

print '<html>'

print '<head>'

print '<title>Hello Word - First CGI Program </title>'

print '</head>'

print '<body>'

print '<h2> Hello Word! Python CGI Program</h2>'

print '</body>'

print '</html>'


CGIHTTPServer 로 구동할 경우 :  python 실행한 결과로 보여집니다.

python -m CGIHTTPServer 8000 .

root@linux-01:/root> curl -i "http://localhost:8000/cgi-bin/hello.py"

HTTP/1.0 200 Script output follows

Server: SimpleHTTP/0.6 Python/2.6.6

Date: Sat, 02 Dec 2017 16:24:04 GMT

Content-type:text/html

<html>

<head>

<title>Hello Word - First CGI Program </title>

</head>

<body>

<h2> Hello Word! Python CGI Program</h2>

</body>

</html>


cgi 는 기본적으로 cgi-bin/폴더를 만든 후 하위 경로에 파이선 스크립트를 작성하여 CGI 스크립트로 작업이 가능하다.

한가지 궁금한 것이 존재하다.


python 을 컴파일 해서 CGI 형태로 호출이 되는지

python -m py_compile hello.py 

빌드 후 컴파일 된 python 파일을 실행해 보자

root@linux-01:/root> curl -i "http://localhost:8000/cgi-bin/hello.pyc"

HTTP/1.0 403 CGI script is not executable ('/cgi-bin/hello.pyc')

Server: SimpleHTTP/0.6 Python/2.6.6

Date: Sat, 02 Dec 2017 16:27:41 GMT

Content-Type: text/html

Connection: close

<head>

<title>Error response</title>

</head>

<body>

<h1>Error response</h1>

<p>Error code 403.

<p>Message: CGI script is not executable ('/cgi-bin/hello.pyc').

<p>Error code explanation: 403 = Request forbidden -- authorization will not help.

</body>

실행 권한이 없어 위와 같이 나온다

chmod 755 hello.pyc 를 한 후 웹으로 호출을 해보자

localhost.localdomain - - [03/Dec/2017 01:30:58] CGI script exit status 0x7f00




HTTP/1.0 200 Script output follows

Server: SimpleHTTP/0.6 Python/2.6.6

Date: Sat, 02 Dec 2017 16:30:58 GMT

----------------------------------------

Exception happened during processing of request from ('127.0.0.1', 56214)

----------------------------------------

200으로 응답은 되나 Exception 이 발생된다.


모듈을 다이렉트로 실행을 요청 하면 실행이 되지 않는다.

해서 다음과 같이 스크립트를 작성 후 모듈을 pyc로 import  하여 실행 하도록 변경하였다.


test.py

#!/usr/bin/evn python

print 'TEST'

python -m py_compile test.py 


├── hello.py

└── test.pyc


hello.py

#!/usr/bin/env python

import test

print "Content-type:text/html\r\n\r\n"

print '<html>'

print '<head>'

print '<title>Hello Word - First CGI Program </title>'

print '</head>'

print '<body>'

print '<h2> Hello Word! Python CGI Program</h2>'

print '</body>'

print '</html>'


root@linux-01:/data/web/cgi-bin> curl -i "http://localhost:8000/cgi-bin/hello.py"

HTTP/1.0 200 Script output follows

Server: SimpleHTTP/0.6 Python/2.6.6

Date: Sat, 02 Dec 2017 17:00:58 GMT

TEST

Content-type:text/html

<html>

<head>

<title>Hello Word - First CGI Program </title>

</head>

<body>

<h2> Hello Word! Python CGI Program</h2>

</body>

</html>








반응형

'시스템' 카테고리의 다른 글

FFmpeg 설치 해보자 - CentOS 6.3  (2) 2018.01.04
메일 서버를 이용해 메일을 보내보자  (0) 2017.12.16
Nginx 설치하기  (0) 2017.11.25
[DNS] /var/named 폴더 경로 변경하기  (0) 2017.11.20
GlusterFS 설치하기  (0) 2017.11.17
Comments