Rumah >pembangunan bahagian belakang >Tutorial Python >Python之POST登录测试
不解释,直接上代码:
<span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">!/usr/bin/env python </span> <span style="color: #008080"> 2</span> <span style="color: #008000">#</span><span style="color: #008000"> -*- encoding: utf-8 -*-</span> <span style="color: #008080"> 3</span> <span style="color: #008080"> 4</span> <span style="color: #800000">"""</span> <span style="color: #008080"> 5</span> <span style="color: #800000">@version: v1.0 </span><span style="color: #008080"> 6</span> <span style="color: #800000">@author: elijahxb </span><span style="color: #008080"> 7</span> <span style="color: #800000">@OS: linux </span><span style="color: #008080"> 8</span> <span style="color: #800000">@contact: elijahxb@163.com </span><span style="color: #008080"> 9</span> <span style="color: #800000">@site: </span><span style="color: #008080">10</span> <span style="color: #800000">@software: PyCharm Community Edition </span><span style="color: #008080">11</span> <span style="color: #800000">@file: zhangye.py </span><span style="color: #008080">12</span> <span style="color: #800000">@time: 17-7-2 下午12:16 </span><span style="color: #008080">13</span> <span style="color: #800000">"""</span> <span style="color: #008080">14</span> <span style="color: #008080">15</span> <span style="color: #800000">'''</span> <span style="color: #008080">16</span> <span style="color: #800000">本次登录测试: </span><span style="color: #008080">17</span> <span style="color: #800000">USERNAME:test001_001 </span><span style="color: #008080">18</span> <span style="color: #800000">PASSWORD:test001 </span><span style="color: #008080">19</span> <span style="color: #800000">URL: </span><span style="color: #008080">20</span> <span style="color: #800000">TYPE: POST </span><span style="color: #008080">21</span> <span style="color: #800000">HOST: www.zhangye.ccoo.cn </span><span style="color: #008080">22</span> <span style="color: #800000">POSTURL: </span><span style="color: #008080">23</span> <span style="color: #800000">POSTDATA:username=test001_001&password=test001 </span><span style="color: #008080">24</span> <span style="color: #800000">'''</span> <span style="color: #008080">25</span> <span style="color: #0000ff">import</span><span style="color: #000000"> httplib </span><span style="color: #008080">26</span> <span style="color: #0000ff">import</span><span style="color: #000000"> urllib </span><span style="color: #008080">27</span> <span style="color: #008080">28</span> HOST = <span style="color: #800000">'</span><span style="color: #800000">182.92.232.234</span><span style="color: #800000">'</span> <span style="color: #008080">29</span> SOURCEURL = <span style="color: #800000">"</span><span style="color: #800000"></span><span style="color: #800000">"</span> <span style="color: #008080">30</span> POSTURL = <span style="color: #800000">'</span><span style="color: #800000"></span><span style="color: #800000">'</span> <span style="color: #008080">31</span> PORT = 80 <span style="color: #008080">32</span> STRICT = False <span style="color: #008000">#</span><span style="color: #008000"> 默认False,表示无法解析服务器返回的状态行时,是否抛出BadStatusLine异常</span> <span style="color: #008080">33</span> TIMEOUT = 5 <span style="color: #008080">34</span> HEADERS =<span style="color: #000000"> { </span><span style="color: #008080">35</span> <span style="color: #800000">'</span><span style="color: #800000">Content-type</span><span style="color: #800000">'</span>: <span style="color: #800000">'</span><span style="color: #800000">application/x-www-form-urlencoded</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #008080">36</span> <span style="color: #800000">"</span><span style="color: #800000">Accept</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">*/*</span><span style="color: #800000">"</span> <span style="color: #008080">37</span> <span style="color: #000000">} </span><span style="color: #008080">38</span> TestDATA =<span style="color: #000000"> { </span><span style="color: #008080">39</span> <span style="color: #800000">'</span><span style="color: #800000">username</span><span style="color: #800000">'</span>: <span style="color: #800000">'</span><span style="color: #800000">test001_001</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #008080">40</span> <span style="color: #800000">'</span><span style="color: #800000">password</span><span style="color: #800000">'</span>: <span style="color: #800000">'</span><span style="color: #800000">test001</span><span style="color: #800000">'</span> <span style="color: #008080">41</span> <span style="color: #000000">} </span><span style="color: #008080">42</span> TestDATA =<span style="color: #000000"> urllib.urlencode(TestDATA) </span><span style="color: #008080">43</span> <span style="color: #008080">44</span> <span style="color: #0000ff">try</span><span style="color: #000000">: </span><span style="color: #008080">45</span> <span style="color: #008000">#</span><span style="color: #008000"> Conn = httplib.HTTPConnection(HOST, PORT, STRICT, TIMEOUT, SOURCEURL)</span> <span style="color: #008080">46</span> HttpClient =<span style="color: #000000"> httplib.HTTPConnection(HOST) </span><span style="color: #008080">47</span> HttpClient.request(<span style="color: #800000">'</span><span style="color: #800000">POST</span><span style="color: #800000">'</span><span style="color: #000000">, POSTURL, TestDATA, HEADERS) </span><span style="color: #008080">48</span> <span style="color: #008080">49</span> response =<span style="color: #000000"> HttpClient.getresponse() </span><span style="color: #008080">50</span> <span style="color: #0000ff">print</span><span style="color: #000000"> response.status </span><span style="color: #008080">51</span> <span style="color: #0000ff">print</span><span style="color: #000000"> response.reason </span><span style="color: #008080">52</span> <span style="color: #0000ff">print</span><span style="color: #000000"> response.read() </span><span style="color: #008080">53</span> <span style="color: #0000ff">print</span><span style="color: #000000"> response.getheaders() </span><span style="color: #008080">54</span> <span style="color: #0000ff">except</span><span style="color: #000000"> Exception, e: </span><span style="color: #008080">55</span> <span style="color: #0000ff">print</span><span style="color: #000000"> e </span><span style="color: #008080">56</span> <span style="color: #0000ff">finally</span><span style="color: #000000">: </span><span style="color: #008080">57</span> <span style="color: #0000ff">if</span><span style="color: #000000"> HttpClient: </span><span style="color: #008080">58</span> HttpClient.close()
Atas ialah kandungan terperinci Python之POST登录测试. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!