Take a friend's web job site as an example:
http://simpledating.sinaapp.com/
Web pages can be accessed using URLLIB2 in the standard library
GET:
Take a look at the normally issued GET request:
The code is as follows:
#coding: Utf-8ImportUrllib2# OpenDebug LogEasy CommissioningHttpHandler = Urllib2. HttpHandler (DebugLevel=1) Httpshandler = Urllib2. Httpshandler (DebugLevel=1) opener = Urllib2.build_opener (HttpHandler, Httpshandler) Urllib2.install_opener (opener)Try: Header = {' Accept ':' */* ', ' Accept-language ':' zh-cn,zh;q=0.8 ', ' Connection ':' keep-alive ', ' Host ':' simpledating.sinaapp.com ', ' Origin ':' http://simpledating.sinaapp.com ', ' Referer ':' http://simpledating.sinaapp.com/', ' User-agent ':' mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/35.0.1916.153 safari/537.36 SE 2.X METASR 1.0 ', }< Span style= "COLOR: #808080" > # add header req = urllib2. Request (url = "Http://simpledating.sina app.com/' headers = header) Response = Urllib2.urlopen (req) print response.read () except Urllib2. Httperror e: print e.code
return: D:\Python\python.exe d:/users/administrator/pycharmprojects/web/get.py
Send: ' Get/http/1.1\r\naccept-encoding:identity\r\norigin:http://simpledating.sinaapp.com\r\naccept-language: Zh-cn,zh;q=0.8\r\nconnection:close\r\naccept: */*\r\nuser-agent:mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/35.0.1916.153 safari/537.36 SE 2.X metasr 1.0\r\nhost:simpledating. Sinaapp.com\r\nreferer:http://simpledating.sinaapp.com/\r\n\r\n '
Reply: ' http/1.1 ok\r\n '
header:server:nginx/1.4.4
Header:Date:Mon, 06:22:26 GMT
header:content-type:text/html; Charset=utf-8
header:content-length:12559
Header:Connection:close
Header:etag: "1688e856917006e409c1932e75e871bf53b2315c"
Header:via:yq34.pyruntime
header:set-cookie:saeut=ckmpilszaciy9kkjcyxeag==; Expires=thu, 31-dec-37 23:55:55 GMT; path=/
<! DOCTYPE html>
<meta charset= "Utf-8" ...
POST:
First look at the POST request that was sent normally:
can easily see the post data format is three, oh this site also added XSRF ( cross-site request forgery ), to prevent the forgery of the request.
Then I'm asking for data and cookies to be forged.
The code is as follows:
#coding: Utf-8ImportUrllibImportUrllib2# OpenDebug LogEasy CommissioningHttpHandler = Urllib2. HttpHandler (DebugLevel=1) Httpshandler = Urllib2. Httpshandler (DebugLevel=1) opener = Urllib2.build_opener (HttpHandler, Httpshandler) Urllib2.install_opener (opener)Try:#datais to submit the data according to the format he requested to fill in data = {u ' _xsrf ':u ' f65fb8fdd4134e1f815c7a10f37561f6 ',u ' place ':u ' ~ ', u ' content ':u ' asdwqwt ', u ' time ':u ' 9999/99/99 ab:cd '} data = Urllib.urlencode (data) header = {' Accept ':' */* ', # ' accept-encoding ': ' Gzip,deflate,sdch ', ' Accept-language ':' zh-cn,zh;q=0.8 ', ' Connection ':' keep-alive ', ' Cookie ':' saeut=ckmpglsn3wdyh1mehbwpag==; user=eglvbmdiawfv|1421041643|f368e242dd53c65621ee754688042ae8898c572b;_ Xsrf=f65fb8fdd4134e1f815c7a10f37561f6 ', ' Host ':' simpledating.sinaapp.com ', ' Origin ':' http://simpledating.sinaapp.com ', ' Referer ':' http://simpledating.sinaapp.com/createBroadDating ', ' User-agent ':' mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/35.0.1916.153 safari/537.36 SE 2.X METASR 1.0 ', # Postto join the: ' Content-type ':' application/x-www-form-urlencoded; Charset=utf-8 ', ' Content-length ':' the ", ' X-requested-with ':' XMLHttpRequest ', } req = Urllib2. Request (URL=' http://simpledating.sinaapp.com/createBroadDating ', Data= Data, Headers= header) Response = Urllib2.urlopen (req)PrintResponse.read ()exceptUrllib2. Httperror, E:Print' ERROR ', E.code
The returned results are as follows:
D:\Python\python.exe d:/users/administrator/pycharmprojects/web/web.py
Send: ' post/createbroaddating http/1.1\r\naccept-encoding:identity\r\norigin:http://simpledating.sinaapp.com\r\ Ncontent-length:92\r\naccept-language:zh-cn,zh;q=0.8\r\nconnection:close\r\naccept: */*\r\nuser-agent:mozilla/ 5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/35.0.1916.153 safari/537.36 SE 2.X metasr 1.0\r\nhost:simpledating. sinaapp.com\r\nx-requested-with:xmlhttprequest\r\ncookie:saeut=ckmpglsn3wdyh1mehbwpag==; User=eglvbmdiawfv|1421041643|f368e242dd53c65621ee754688042ae8898c572b;_xsrf=f65fb8fdd4134e1f815c7a10f37561f6\r \nreferer:http://simpledating.sinaapp.com/createbroaddating\r\ncontent-type:application/x-www-form-urlencoded; charset=utf-8\r\n\r\ncontent=asdwqwt&_xsrf=f65fb8fdd4134e1f815c7a10f37561f6&place=%7e&time=9999% 2F99%2F99+AB%3ACD '
Reply: ' http/1.1 ok\r\n '
header:server:nginx/1.4.4
Header:Date:Mon, 06:49:43 GMT
header:content-type:text/html; Charset=utf-8
Header:content-length:7
Header:Connection:close
Header:via:yq34.pyruntime
Success
Visible post is successful.
Use Python to make Post/get requests to the Web site