Based on the understanding of the basic syntax of Python, it is possible to freely assemble a variety of advanced functions that you need.
Because of the characteristics of Python language, the implementation of various functions will be very fast. The network becomes one of the advanced features that Python has. Need to do network programming first need to understand several modules Urllib and URLLIB2 1. Simple Access Request
ImportSys,urllib,urllib2url=input ("Please input the URL:") URL="http://mail.126.com"#initiating a requestreq =Urllib2. Request (URL) fd=Urllib2.urlopen (req)#Output Results#print fd.read ()Print("URL Retrieved:", Fd.geturl ()) Info=Fd.info () forKey, ValueinchInfo.items ():Print "%s =%s"%(Key,value) whileTrue:data= Fd.read (1024) if notlen (data): Break #Print Data
2. A simple GET request
#construct a Get parameter#zipcode = sys.argv[1]WD = input ("Search Word:") Data= Urllib.urlencode ([('WD', WD)])#constructing URL Stitching request ParametersUrl="http://www.baidu.com"URL= URL +"?"+DataPrint 'ursing URL', the URL#constructs the request and requestsreq =Urllib2. Request (URL) fd=Urllib2.urlopen (req)#read the corresponding results whileTrue:data= Fd.read (1024) if notlen (data): Break #sys.stdout.write (data) PrintData
3. The biggest difference between a simple post request and a GET request is that the request parameters are placed in the request body instead of stitching into the URL
ImportSys,urllib,urllib2#Construction Parameters#zipcode = sys.argv[1]WD = input ("Search Word:") Data= Urllib.urlencode ([('WD', WD)])#constructing URLs does not need to stitch parameters into URLsUrl="http://www.baidu.com"Print 'ursing URL', the URL#To construct the request, simply place the parameter in the second parameter of the Urlopenreq =Urllib2. Request (URL) fd=Urllib2.urlopen (req,data)#read the corresponding results whileTrue:data= Fd.read (1024) if notlen (data): Break #sys.stdout.write (data) PrintData
4. Login verification features such as login verification there are many ways, the most common is the post form or the form of a cookie login verification. This type of validation and POST request may be more closely related. Here is the most basic HTTP authentication, the client sends the user name password to the server side. The form usually pops up a landing window
ImportSys,urllib,urllib2,getpass#defines the Terminalpwd class extension httppasswordmgr, which allows you to ask the operator to enter a password when neededclassterminalpwd (urllib2. Httppasswordmgr):defFind_user_password (Self,realm,authuri): retval=Urllib2. Httppasswordmgr.find_user_password (Self,realm,authuri)ifRetval[0] = = None andRETVAL[1] = =None:#didn ' t find it in stored valuesUsername = input ("Login required,please input username:") Password= Input ("Please input password:") return(Username,password)Else: returnRetvalurl="http://home.asiainfo.com/"req=Urllib2. Request (URL)#Need to use opener when additional processing needs to be loaded, such as the need to support authentication processing here#If authentication is required, the functions inside the terminalpwd are automatically called, if no further checks are required, as with ordinary requestsOpener =Urllib2.build_opener (urllib2. Httpbasicauthhandler (Terminalpwd ()))#RequestFD =Opener.open (req)Print("URL Retrieved:", Fd.geturl ()) Info=Fd.info () forKey, ValueinchInfo.items ():Print "%s =%s"%(Key,value)
Python Practice Network Programming 1-Simple network requestor