---
title: "python 模拟POST"
author: "hereson3"
date: "2010-01-07T22:21:55Z"
tags: []
categories: ["编程语言"]
slug: "python-post"
draft: false
lastmod: "2010-01-07T22:21:55Z"
id: 565324
url: "https://kungsoft.iteye.com/blog/565324"
---

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
(
)
```