python用socket類比post請求

來源:互聯網
上載者:User

標籤:ram   串連符   ==   import   xxx   end   str   int   gen   

今天用python的socket類比了下post請求,通過這個執行個體可以更加瞭解python中socket的使用,以及http請求和socket的相互關係等知識.

#coding=utf-8import socket

if __name__=="__main__": s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("www.xxxxx.com",80))
  #pyhon 字串很長時可以用下面這種括弧加雙引號的方式, 這樣也不用像PHP那樣 多行字串需要用串連符串連起來 。
str=("POST /login/log HTTP/1.1\r\n" "Host: www.xxxxx.com\r\n" "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0\r\n" "Referer: http://www.xxxxx.com/\r\n" "Content-Length: 17\r\n" "Cookie: session=f28edeacd71e759d05a80bb3cd9c91856952b3e3%7E58eb987a9efb26-19897462\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Connection: keep-alive\r\n\r\n" "param1=a&param2=b" )  # post 的關鍵是頭部的 Content-Type: application/x-www-form-urlencoded\r\n 這行,之前唯寫了x-www-form-urlencoded,提交過去,後端php列印$_POST一直為空白
  # Content-Length: 17\r\n 的17是內容裡要post的參數的長度(即param1=a&param2=b),注意頭部和內容要用 \r\n\r\n 分開  
    s.send(str)    data=s.recv(2084)    print data    
  #伺服器一般發送完訊息後,經過確認會主動斷開socket s.close()

 

python用socket類比post請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.