python 網路編程筆記(一)-WEB用戶端__編程

來源:互聯網
上載者:User

1.一般請求:

<pre name="code" class="python">req = urllib2.Request("http://www.baidu.com")fd = urllib2.urlopen(req)while 1:data = fd.read(1024)if not len(data):breakprint data

2. 認證 (TBD)

 
3. 表單:
    GET:1) 與一般請求相似,手工把GET參數添加到URL中,<span style="font-family: Arial, Helvetica, sans-serif;">手工構造URL時空格應該換成“+”。 構造的過程可能非常的複雜。
req = urllib2.Request("http://www.freebsd.org/cgi/search.cgi?words=python+socket&max=25&source=www")fd = urllib2.urlopen(req)
2) 使用urllib.urlencode()方法
 
   def addGETdata(url, data):return url + '?' + urllib.urlencode(data)      def get_method_in_form2():#paramzipcode = [("words", "python socket"), ("max", "25") ,("source", "www")]url = addGETdata('http://www.freebsd.org/cgi/search.cgi', zipcode)req = urllib2.Request(url)fd = urllib2.urlopen(req)



 
    POST:一樣是使用urllib.urlencode()方法對參數編碼,urllib2.urlopen()方法發送請求,只是參數不是放在URL中, 而是做為urlopen的第二個參數
zipcode = [("query", "10001")]data = urllib.urlencode(zipcode)url = 'http://www.wunderground.com/cgi-bin/findweather/getForecast'req = urllib2.Request(url)fd = urllib2.urlopen(req, data)
4 錯誤處理 (TBD)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.