ITeye博客

python 模拟POST

hereson3 · 2010-01-07 22:21 · ActionScript · 编程语言 · Markdown

python使用urllib和httplib库模拟提交表单。

比如登录[login.html]界面是

<form action="" method="POST">
  <input name="user"/>
  <input name="passwd"/>
</form>

python模拟login登录的时候可以作如下代码:

import
 urllib
, httplib

url = 'localhostl'

path = '/login.html'

headers = {
"Accept"
:"text/html"
,"User-Agent"
:"ShopEx_SAAS"
,"Content-type"
:"application/x-www-form-urlencoded"
}

conn = httplib
.HTTPConnection
(
url)

params = {
'user'
:'tito'
,'passwd'
:'tito'
}

try
:
    conn.request
(
"POST"
,path,params,headers)

    r=conn.getresponse
(
)

    if
 r.status
 is
 not
 200
:
        print
 'download goods error'

        sys
.exit
(
)

except
 urllib2
.URLError
,IOError
: 
    print
 URLError
    sys
.exit
(
)

print
 r.read
(
)

评论(1)

  1. #1 楼 · 雷蕾_ing · 2012-05-23 15:27

    :lol:

评论功能维护中,当前仅展示历史评论。