`
san_yun
  • 浏览: 2652775 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

urlparse,httplib,urllib和urllib2

 
阅读更多
参考:http://www.cnblogs.com/chenzehe/archive/2010/08/30/1812995.html
http://www.cnblogs.com/txw1958/archive/2012/03/12/2392067.html

通过urlparse提供了强大的url解析能力:
>>> url="http://localhost/test.py?a=hello&b=world "
>>> result=urlparse.urlparse(url)
>>> result
ParseResult(scheme='http', netloc='localhost', path='/test.py', params='', query='a=hello&b=world ', fragment='')
>>> urlparse.parse_qs(result.query,True)
{'a': ['hello'], 'b': ['world ']}
>>> params=urlparse.parse_qs(result.query,True)
>>> params
{'a': ['hello'], 'b': ['world ']}
>>> params['a'],params['b']
(['hello'], ['world '])


urlparse
result = urlparse.urlparse("http://item.taobao.com/item.htm?id=14952833371")
>>> result.path
'/item.htm'
>>> result.query
'id=14952833371'
>>> result.hostname
'item.taobao.com'
>>> params = urlparse.parse_qs(result.query)
>>> dict: {u'id': [u'14952833371']}


import urllib2
import urllib

#request = urllib2.Request(url="http://0.0.0.0:8080/",headers={"FUCK":"fuck"})
#content = urllib2.urlopen(request)

values ={'name' : 'test short talk','via':'xxxx'}
form_data = urllib.urlencode(values) 
content = urllib2.urlopen("http://0.0.0.0:8080/", data=form_data, timeout=3)
print content.readline()
content.close()



import urllib2
import urllib
import httplib
import string
from struct import unpack
from struct import pack


type, uri = urllib.splittype("http://0.0.0.0:8080/hello")
print type+"\t"+uri
host, uri = urllib.splithost(uri)
print host+"\t"+uri

h = httplib.HTTP(host)
h.putrequest("POST", uri)
h.putheader("Host", host)
h.putheader("User-Agent","hessianlib.py")
h.endheaders()
values ={'name' : 'test short talk','via':'xxxx'}  
form_data = urllib.urlencode(values)   

out = []
write = write = out.append
method = "hello"
#write("c\x01\x00m");
write(pack(">H", len(method)));
write(method)
result = string.join(out, "")    
print "result:"+result
h.send(result)


result = urlparse.urlparse(buyable_links['taobao'])
params=urlparse.parse_qs(result.query, True)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics