学习下http的基本用法
客户端主要用到httplib模块,使用客户端访问编写的服务端。
先看客户端代码
class HTTPClient(object):
def __init__(self, host):
self.host = host
def fetch(self, path):
http = httplib.HTTP(self.host)
# Prepare header
http.putrequest("GET", path)
http.putheader("User-Agent", __file__)
http.putheader("Host", self.host)
http.putheader("Accept", "*/*")
http.endheaders()
try:
errcode, errmsg, headers = http.getreply()
except Exception, e:
print "Client failed error code: %s message: %s headers: %s" % (errcode, errmsg, headers)
else:
print "Got homepage from %s" % self.host
file = http.getfile()
return file.read()
if __name__ == "__main__":
client = HTTPClient("192.168.199.110:9999")
print client.fetch("")
服务端
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", 'text/html')
self.end_headers()
# Send message to browser
self.wfile.write("Hello from server!")
return
class CustomHTTPServer(HTTPServer):
def __init__(self, host, port):
server_address = (host, port)
HTTPServer.__init__(self, server_address, RequestHandler)
def run_server(port):
try:
server = CustomHTTPServer('192.168.199.110', port)
print "Custom HTTP server started on port: %s" % port
server.serve_forever()
except Exception, e:
print "Error: %s" % e
except KeyboardInterrupt:
print "Server interrupted and is shutting down..."
server.socket.close()
if __name__ == '__main__':
run_server(9999)
启动服务端,开启客户端去访问,能得到服务端返回的信息。基本套路就完了,下面看下cookie的获取,这里获取一下自己csdn的cookie
import cookielib
import urllib
import urllib2
ID_USERNAME = 'id_username'
ID_PASSWORD = 'id_password'
USERNAME = 'hexiaodouaipiqiu'
PASSWORD = '*******'
LOGIN_URL = 'https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn'
NORMAL_URL = 'http://www.csdn.net/'
def extract_cookie_info():
"Fake login to a site with cookie"
cj = cookielib.CookieJar()
login_data = urllib.urlencode({ID_USERNAME: USERNAME, ID_PASSWORD: PASSWORD})
# create url opener
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
resp = opener.open(LOGIN_URL, login_data)
# Send login info
for cookie in cj:
print "----First time cookie: %s --> %s" % (cookie.name, cookie.value)
print "Headers: %s" % resp.headers
# now access without any login info
resp = opener.open(NORMAL_URL)
for cookie in cj:
print "++++Second time cookie: %s --> %s" % (cookie.name, cookie.value)
print "Headers: %s" %resp.headers
if __name__ == '__main__':
extract_cookie_info()
纪录下登陆的表单需要哪些信息,csdn只需要账户及密码。然后用cookielib模块创建了一个cookie容器cj。登陆数据使用了urllib.urlencode方法编码。
urllib2模块中有个build_opener方法,其中的参数是一个HTTPCookieProcessor类实例。我们需要把之前创建的cookie容器传给这个类的构造方法。urllib2.build_opener方法返回值是一个URL打开方法。需要调用这个方法两次:第一次登陆页面,第二次访问网站首页。