There are many types of interfaces, but we often meet the two kinds of get and post that we often use. What is the difference between the two? Personal understanding is mainly expressed in terms of safety.
Python code post arbitrary HTTP data and the method of using cookies, the need for friends can refer to.
1), when not using cookies, it is very simple to send an HTTP post:
Import URLLIB2, Urllib data = {' name ': ' www ', ' password ': ' 123456 '}f = urllib2.urlopen ( url = ' Http://www.haibia n.com/', data = Urllib.urlencode (data) ) print F.read ()
2), when using cookies, the code becomes somewhat more complex:
Import Urllib2 cookies = Urllib2. Httpcookieprocessor () opener = Urllib2.build_opener (cookies) F = opener.open (' http://www.haibian.com ') data = ' <root >Hello</root> ' request = Urllib2. Request ( url = ' http://www.haibian.com/index/ajax_login ', headers = {' Content-type ': ' Text/xml '}, Data = data) Opener.open (Request)
Annotations:
The first open () is to log in. The cookie returned by the server is automatically saved in the cookie and is used for subsequent requests.
The second open () sends the CONTENT-TYPE=TEXT/XML data to the server using the POST method. If you do not create a Request, but instead use the Urlopen () method directly, Python forces the Content-type to be changed to application/x-www-form-urlencoded.
Python implements interface testing