from urllib2 import urlopen
f = urlopen('http://www.example.com')
print f.read(500)
<!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin:
from urllib2 import urlopen
data = 'query=python'
f = urlopen('http://www.example.com', data)
print f.read(300)
<!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background
import urllib2
req = urllib2.Request('http://www.example.com')
req.add_header('Content-Type', 'text/plain')
req.add_data('query=python')
f = urllib2.urlopen(req)
print f.read(300)
<!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background
from urllib2 import urlopen
from HTMLParser import HTMLParser
class ImageParser(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag != 'img':
return
if not hasattr(self, 'result'):
self.result = []
for name, value in attrs:
if name == 'src':
self.result.append(value)
def parseImage(data):
parser = ImageParser()
parser.feed(data)
dataSet = set(x for x in parser.result)
print '\n'.join(sorted(dataSet))
def main():
url = 'http://www.google.co.kr'
f = urlopen(url)
charset = f.info().getparam('charset')
data = f.read().decode(charset)
f.close()
print '\n>>>>>>> Fetch Images from', url
parseImage(data)
main()
>>>>>>> Fetch Images from http://www.google.co.kr /images/icons/product/chrome-48.png /logos/doodles/2015/155th-anniversary-of-the-pony-express-5959391580782592.2-hp.jpg /textinputassistant/tia.png
import httplib
conn = httplib.HTTPConnection('www.example.com')
conn.request('GET', '/index.html')
r1 = conn.getresponse()
r1.status, r1.reason
(200, 'OK')
data1 = r1.read()
print data1
<!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 50px; background-color: #fff; border-radius: 1em; } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { body { background-color: #fff; } div { width: auto; margin: 0 auto; border-radius: 0; padding: 1em; } } </style> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.</p> <p><a href="http://www.iana.org/domains/example">More information...</a></p> </div> </body> </html>
conn.request('GET', '/parrot.spam')
r2 = conn.getresponse()
print r2.status, r2.reason
404 Not Found
data2 = r2.read()
print data2
<!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 50px; background-color: #fff; border-radius: 1em; } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { body { background-color: #fff; } div { width: auto; margin: 0 auto; border-radius: 0; padding: 1em; } } </style> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.</p> <p><a href="http://www.iana.org/domains/example">More information...</a></p> </div> </body> </html>
conn.close()
import httplib
conn = httplib.HTTPConnection('www.example.com')
conn.request('HEAD', '/index.html')
res = conn.getresponse()
print res.status, res.reason
200 OK
data = res.read()
print len(data)
0
data == ''
True
import httplib, urllib
params = urllib.urlencode({'@number': 12524,
'@type': 'issue',
'@action': 'show'})
headers = {'Content-type': 'application/x-www-form-urlencoded',
'Accept': 'text/plain'}
conn = httplib.HTTPConnection('bugs.python.org')
conn.request('POST', '', params, headers)
response = conn.getresponse()
print response.status, response.reason
302 Found
data = response.read()
print data
Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>
conn.close()
import httplib
BODY = '***filecontents***'
conn = httplib.HTTPConnection('localhost', 8888)
conn.request('PUT', '/file', BODY)
response = conn.getresponse()
print response.status, response.reason
404 Not Found
import httplib
from urlparse import urljoin, urlunparse
from urllib import urlretrieve
from HTMLParser import HTMLParser
import os
class ImageParser(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag != 'img':
return
if not hasattr(self, 'result'):
self.result = []
for name, value in attrs:
if name == 'src':
self.result.append(value)
def downloadImage(srcUrl, data):
if not os.path.exists('DOWNLOAD'):
os.makedirs('DOWNLOAD')
parser = ImageParser()
parser.feed(data)
resultSet = set(x for x in parser.result)
for x in sorted(resultSet):
src = urljoin(srcUrl, x)
basename = os.path.basename(src)
targetFile = os.path.join('DOWNLOAD', basename)
print 'Downloading...', src
urlretrieve(src, targetFile)
def main():
host = 'www.google.co.kr'
conn = httplib.HTTPConnection(host)
conn.request('GET', '')
resp = conn.getresponse()
charset = resp.msg.getparam('charset')
data = resp.read().decode(charset)
conn.close()
print '\n>>>>>>> Download Images from', host
url = urlunparse(('http', host, '', '', '', ''))
downloadImage(url, data)
main()
>>>>>>> Download Images from www.google.co.kr Downloading... http://www.google.co.kr/images/icons/product/chrome-48.png Downloading... http://www.google.co.kr/logos/doodles/2015/155th-anniversary-of-the-pony-express-5959391580782592.2-hp.jpg Downloading... http://www.google.co.kr/textinputassistant/tia.png
!ls -l DOWNLOAD/
total 160 -rw-r--r-- 1 re4lfl0w staff 72178 4 14 06:12 155th-anniversary-of-the-pony-express-5959391580782592.2-hp.jpg -rw-r--r-- 1 re4lfl0w staff 1834 4 14 06:12 chrome-48.png -rw-r--r-- 1 re4lfl0w staff 387 4 14 06:12 tia.png
!open DOWNLOAD/chrome-48.png
!open DOWNLOAD/tia.png
!open DOWNLOAD/155th-anniversary-of-the-pony-express-5959391580782592.2-hp.jpg
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.wfile.write('Hello World')
if __name__ == '__main__':
server = HTTPServer(('', 8000), MyHandler)
print 'Started WebServer on port 8888...'
print 'Press ^C to quit WebServer'
server.serve_forever()
Started WebServer on port 8888... Press ^C to quit WebServer
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-60-130adc78dd87> in <module>() 9 print 'Started WebServer on port 8888...' 10 print 'Press ^C to quit WebServer' ---> 11 server.serve_forever() /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.pyc in serve_forever(self, poll_interval) 234 # shutdown request and wastes cpu at all other times. 235 r, w, e = _eintr_retry(select.select, [self], [], [], --> 236 poll_interval) 237 if self in r: 238 self._handle_request_noblock() /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.pyc in _eintr_retry(func, *args) 153 while True: 154 try: --> 155 return func(*args) 156 except (OSError, select.error) as e: 157 if e.args[0] != errno.EINTR: KeyboardInterrupt:
모듈명 | 모듈에서 정의하고 있는 내용 | 처리 기능 |
---|---|---|
BaseHTTPServer | - 기반 서버 클래스용으로, HTTPServer 정의 - 핸들러 클래스용으로, BaseHTTPRequestHandler 정의 - 테스트용 웹 서버를 실행하는 함수, test() 정의 |
기반클래스로, HTTP 프로토콜 처리 |
SimpleHTTPServer | - 기반 서버 클래스인 HTTPServer를 임포트하여 사용 - 핸들러 클래스용으로, SimpleHTTPRequestHandler 정의 - 테스트용 웹 서버를 실행하는 함수, test() 정의 |
GET과 HEAD 메소드 처리 가능 |
CGIHTTPServer | - 기반 서버 클래스인 HTTPServer를 임포트하여 사용 - 핸들러 클래스용으로, CGIHTTPRequestHandler 정의 - 테스트용 웹 서버를 실행하는 함수, test() 정의 |
POST와 CGI 처리 가능 |
!python -m SimpleHTTPServer 8001
Serving HTTP on 0.0.0.0 port 8001 ... 127.0.0.1 - - [15/Apr/2015 23:20:55] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [15/Apr/2015 23:20:56] code 404, message File not found 127.0.0.1 - - [15/Apr/2015 23:20:56] "GET /favicon.ico HTTP/1.1" 404 - ^CTraceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleHTTPServer.py", line 224, in <module> test() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleHTTPServer.py", line 220, in test BaseHTTPServer.test(HandlerClass, ServerClass) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 599, in test httpd.serve_forever() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 236, in serve_forever poll_interval) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 155, in _eintr_retry return func(*args) KeyboardInterrupt
!python -c 'import SimpleHTTPServer; SimpleHTTPServer.test()' 8001
Serving HTTP on 0.0.0.0 port 8001 ... 127.0.0.1 - - [15/Apr/2015 23:21:32] "GET / HTTP/1.1" 200 - ^C---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 56218) Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock self.process_request(request, client_address) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request self.finish_request(request, client_address) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request self.RequestHandlerClass(request, client_address, self) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 649, in __init__ self.handle() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 340, in handle self.handle_one_request() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 310, in handle_one_request self.raw_requestline = self.rfile.readline(65537) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 476, in readline data = self._sock.recv(self._rbufsize) KeyboardInterrupt ----------------------------------------
!python -m CGIHTTPServer 8001
Serving HTTP on 0.0.0.0 port 8001 ... 127.0.0.1 - - [15/Apr/2015 23:23:47] "GET / HTTP/1.1" 200 - ^CTraceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/CGIHTTPServer.py", line 377, in <module> test() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/CGIHTTPServer.py", line 373, in test SimpleHTTPServer.test(HandlerClass, ServerClass) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleHTTPServer.py", line 220, in test BaseHTTPServer.test(HandlerClass, ServerClass) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 599, in test httpd.serve_forever() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 236, in serve_forever poll_interval) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 155, in _eintr_retry return func(*args) KeyboardInterrupt
import urllib2
from urllib import urlencode
url = 'http://localhost:8001/cgi-bin/script.py'
data = {
'language' : 'python',
'framework': 'django',
'email': '[email protected]'
}
encData = urlencode(data)
f = urllib2.urlopen(url, encData) # POST
print f.read()
Welcome, CGI Scripts language is python framework is django email is [email protected]
def application_name(environ, start_response):
start_response(status, headers)
def my_app(environ, start_response):
status = '200 OK'
headers = [('Content-Type', 'text/plain')]
start_response(status, headers)
return ['This is a sample WSGI Application.']
if __name__ == '__main__':
from wsgiref.simple_server import make_server
print 'Started WSGI Server on port 8001...'
server = make_server('', 8001, my_app)
server.serve_forever()