Post Forms and Upload Files using Linux Command Line
I need to Post Forms and Upload Files to a website using Linux Command Line.
I have done some search, and I w开发者_运维技巧ould like to write the script in Python.
I need to first log in the website, save the cookies and then post form data and upload files to that website.
Here is the details:
The website's log in page is:hxxp://www.example.com/login.html
<form action="/signin.html" method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="hidden" name="referer" value="http://www.example.com/">
<input type="submit" name="submit" value=" Login ">
</form>
The upload page is:hxxp://www.example.com.com/upload/
<form action="http://www.example.com:81/upload/upload.cgi" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="314572800" />
File:
<input name="filename[]" type="file" />
Type:
<input type="radio" name="typeID" value=1> Option One
<input type="radio" name="typeID" value=2> Option Two
<input type="radio" name="typeID" value=3> Option Three
Title:
<input type="text" name="title" >
Description:
<textarea name="description"></textarea>
<input type="checkbox" name="agree" value="agree_terms"> I Accept Terms
<input type="submit" value="Upload It!">
</form>
This form contains radio, text, checkbox, file etc input.
Please give me some hint!
I am using CentOS 5.5, with Python, wget, PHP installed. I think this can be done in Python script.
Thanks a lot! Your answer will be the best Christmas gift I have received. ;)
You could of course use urllib2
to accomplish this task. Read the docs on how to deal with cookies and how to upload files. However, I think using mechanize
could save a lot of time. Mechanize allows you to deal with webpages as if you were using a browser:
import mechanize
br = mechanize.Browser()
br.open("http://www.example.com/")
br.select_form()
br['username'] = 'user'
br['password'] = 'pass'
br.submit()
etc.
精彩评论