python 学习笔记15-----web编程

一、3个常用的web模块,urlparse、urllib、urllib2

urlparse最常用的函数就是urlparse,给定url,将url分割成各个不用部分,如下所示。

>>> urlparse.urlparse("http://v.ml.streamocean.com/live/JSTV-HZ-BAK?fmt=x264_400K_flv")
ParseResult(scheme='http', netloc='v.ml.streamocean.com', path='/live/JSTV-HZ-BAK', params='', query='fmt=x264_400K_flv', fragment='')

urllib

urlopen()

二、urllib2用例

这里给出了一个发起post和get的http client,并给出wireshark抓包内容。

import urlparse
import urllib
import json
import urllib2

def post_count(values):  
    url = "http://172.16.1.212:8800/api/payment_info"
    req = urllib2.Request(url, json.dumps(values)) 
    res = urllib2.urlopen(req)
    ret_code = res.code
    html_body   = res.read()
    print("post: %s", values)
    print("ret_code: %d, %s", ret_code, html_body)   


def get_count():
    req = urllib2.Request("http://172.16.1.212:8800/api/payment_info")
    req.add_header("User-Agent", "Test")
    res = urllib2.urlopen(req)
    content = res.read()
    account_list = json.loads(content)
    print account_list


values = {
          "ver":"1",
          "vendor":"7PO",
          "pkg_id":"2"
}
post_count(values)  
get_count()
抓包查看get和post http消息头如下:
GET /api/payment_info HTTP/1.1
Accept-Encoding: identity
Host: 172.16.1.212:8800
Connection: close
User-Agent: Test



POST /api/payment_info HTTP/1.1
Accept-Encoding: identity
Content-Length: 44
Host: 172.16.1.212:8800
Content-Type: application/x-www-form-urlencoded
Connection: close
User-Agent: Python-urllib/2.7

{"pkg_id": "2", "vendor": "7PO", "ver": "1"}

  1. Request的第二个参数,如果为空,则为get请求,如果不为空,则为post请求,同时,第二个参数将是需要post的内容;
  2. 注意上面例子是通过json将字典整合,得到的post内容为{"pkg_id": "2", "vendor": "7PO", "ver": "1"}。网上还有种方法是urllib.urlencode(values),其得到的内容为-----pkg_id=2&vendor=7PO&ver=1,显然前者更常用些;
  3. 如果要上传内容,post请求,是常用的方法,当然,通过get请求,将需要上报的内容放到url中,也是可以的。

一个简单的http server和http client

#!/usr/bin/env python

from os import curdir, sep
from BaseHTTPServer import \
    BaseHTTPRequestHandler, HTTPServer

class MyHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            f = open(curdir + sep + self.path)
            self.send_response(200)
            self.send_header('Content-type',
		'text/html')
            self.end_headers()
            self.wfile.write(f.read())
            f.close()
        except IOError:
            self.send_error(404,
		'File Not Found: %s' % self.path)

def main():
    try:
        server = HTTPServer(('', 80), MyHandler)
        print 'Welcome to the machine...'
	print 'Press ^C once or twice to quit'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()

if __name__ == '__main__':
    main()
import urlparse
import urllib
import json
import urllib2
def get_count():
    req = urllib2.Request("http://172.16.1.212:8888/friends.htm")
    req.add_header("User-Agent", "Test")
    res = urllib2.urlopen(req)
    content = res.read()
    #account_list = json.loads(content)
    print content
get_count()

<HTML><HEAD><TITLE>
Friends CGI Demo (static screen)
</TITLE></HEAD>
<BODY><H3>Friends list for: <I>NEW USER</I></H3>
<FORM ACTION="/cgi-bin/friends1.py">
<B>Enter your Name:</B>
<INPUT TYPE=text NAME=person VALUE="NEW USER" SIZE=15>
<P><B>How many friends do you have?</B>
<INPUT TYPE=radio NAME=howmany VALUE="0" CHECKED> 0
<INPUT TYPE=radio NAME=howmany VALUE="10"> 10
<INPUT TYPE=radio NAME=howmany VALUE="25"> 25
<INPUT TYPE=radio NAME=howmany VALUE="50"> 50
<INPUT TYPE=radio NAME=howmany VALUE="100"> 100
<P><INPUT TYPE=submit></FORM></BODY></HTML>

抓包内容:

GET /friends.htm HTTP/1.1
Accept-Encoding: identity
Host: 172.16.1.212:8888
Connection: close
User-Agent: Test

HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.6.8
Date: Fri, 21 Feb 2014 08:21:28 GMT
Content-type: text/html

<HTML><HEAD><TITLE>
Friends CGI Demo (static screen)
</TITLE></HEAD>
<BODY><H3>Friends list for: <I>NEW USER</I></H3>
<FORM ACTION="/cgi-bin/friends1.py">
<B>Enter your Name:</B>
<INPUT TYPE=text NAME=person VALUE="NEW USER" SIZE=15>
<P><B>How many friends do you have?</B>
<INPUT TYPE=radio NAME=howmany VALUE="0" CHECKED> 0
<INPUT TYPE=radio NAME=howmany VALUE="10"> 10
<INPUT TYPE=radio NAME=howmany VALUE="25"> 25
<INPUT TYPE=radio NAME=howmany VALUE="50"> 50
<INPUT TYPE=radio NAME=howmany VALUE="100"> 100
<P><INPUT TYPE=submit></FORM></BODY></HTML>

当然也可以通过浏览器来查看,直接查看http://172.16.1.212:8888/friends.htm,可以看到html页面。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值