python構造 python構造乙個http請求

2021-10-11 15:05:49 字數 1575 閱讀 1951

我們經常會用python來進行抓包,模擬登陸等等, 勢必要構造http請求包。

http的request通常有4個方法get,post,put,delete,分別對應於查詢,更新,新增,刪除。我們經常用到的也就get,post。

1.用python構造get

2.用python構造post

#build request for login url

#post data

postdata = {

'u':username,

'p':password,

'remember':'1',

't':'log',

'f':urllib.parse.quote(moduleurl),

postdata = urllib.parse.urlencode(postdata).encode('utf-8')

print(postdata)

req = urllib.request.request(

url = csdnloginurl,

data = postdata)

req.add_header('accept-language', 'en-us')

req.add_header('accept-encoding', 'gzip, deflate')

req.add_header('connection', 'keep-alive');

req.add_header('referer', csdnaccessmoduleurl)

req.add_header('user-agent', 'mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; wow64; trident/5.0)');

#open login url

r = urllib.request.urlopen(req)

通過上面2個例子我們可以發現, 當request的postdata賦值時,則為post方法,預設為get。

python物件導向與構造構析函式

程式設計正規化 程式設計范型 程式設計正規化或程式設計法 範即模範 典範之意,正規化即模式 方法 是一類典型的程式設計風格,是指從事軟體工程的一類典型的風格 可以對照方法學 如 函式式程式設計 程式程式設計 物件導向程式設計 命令式程式設計等等為不同的程式設計范型。面向過程程式設計 面向過程 pro...

Python重寫類的構造 析構方法

python提供了預設的構造 析構方法,在需要時可以對其進行重寫,如下。class washer 初始化方法,類似於構造方法 def init self,width,height print 執行初始化方法 self.width width self.height height 定義列印例項物件的字...

使用Python定義建構函式和析構函式

定義類 class student 名稱name 張三 構造方法 def init self print 構造方法被呼叫 析構方法 def del self print 析構方法被呼叫 自我介紹的方法 def show self print 你好 我是 s self.name 例項化物件 zhang...