- Python Penetration Testing Cookbook
- Rejah Rehim
- 196字
- 2021-07-02 23:08:41
With Python 3
In Python 3 both urllib and urllib2 are part of the urllib module, so there is some difference in using urllib. Also, the urllib package contains the following modules:
- urllib.request
- urllib.error
- urllib.parse
- urllib.robotparser
The urllib.requestmodule is used for opening and fetching URLs with Python 3:
- First import the urllib.request module from urllib packages:
>>> import urllib.request
- Get the web page with the urlopen method:
>>> webpage = urllib.request.urlopen("https://www.packtpub.com/")
- Read the object with the read method:
>>> source = webpage.read()
- Close the object:
>>> webpage.close()
- Print the source:
>>> print(source)
- You can write the contents of the source string to a local file on your computer as follows. Make sure that the output file is in binary mode:
>>> f = open('packtpub-home.html', 'wb') >>> f.write(source) >>> f.close
Python 2 modules urllib and urllib2 help to do URL-request-related stuff, but both have different functionalities.
urllib provides the urlencode method, which is useful in generating GET requests. However, urllib2 doesn't support the urlencode method. Also, urllib2 can accept the request object and modify the headers for a URL request, but urllib can only accept the URL and is not capable of modifying the headers in it.
推薦閱讀
- 網(wǎng)絡(luò)安全與管理
- 數(shù)據(jù)恢復(fù)方法及案例分析
- CTF實戰(zhàn):技術(shù)、解題與進階
- 工業(yè)互聯(lián)網(wǎng)安全防護與展望
- Practical Network Scanning
- 計算機使用安全與防護
- 模糊測試:強制發(fā)掘安全漏洞的利器
- Applied Network Security
- 從實踐中學(xué)習(xí)Kali Linux滲透測試
- Cybersecurity Threats,Malware Trends,and Strategies
- 計算機系統(tǒng)與網(wǎng)絡(luò)安全研究
- 黑客攻防與電腦安全從新手到高手(超值版)
- 密碼朋克:自由與互聯(lián)網(wǎng)的未來
- 2010年中國互聯(lián)網(wǎng)網(wǎng)絡(luò)安全報告
- 網(wǎng)絡(luò)安全攻防技術(shù)實戰(zhàn)