Отправка http-запроса на upload-файла на Yandex.narod2
- #отправка HTTP запроса на вход на яндекс.народ2
- conn = httplib.HTTPConnection("passport.yandex.ru")
- params = urllib.urlencode({'login': "xxxxxxxxxx", 'passwd': "yyyyyyyy"})
- headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/html"}
- conn.request("POST", "/passport?mode=login&retpath=http://хххххххх.narod2.yandex.ru/edit/", params, headers)
- r1 = conn.getresponse()
- print r1.status, r1.reason
- data1 = r1.read()
- #print data1
- conn.close()
- #парсим куки ответа
- cookp = data1.find('Set-Cookie: ')
- cookend = 0;
- cooks = "Cookie: "
- while cookp > 0 :
- cookp += 12
- cookend = data1.find(';',cookp)
- cooks += data1[cookp:cookend]
- cookp = data1.find('Set-Cookie: ',cookend)
- if cookp > 0 :
- cooks += "; "
- print(str(cooks))
- #шлём файл
- print("\n\n")
- conn = httplib.HTTPConnection("xxxxxxxxxxx.narod2.yandex.ru")
- postbody = '''--bndr777
- Content-Disposition: form-data; name="action"
- upload
- --bndr777
- Content-Disposition: form-data; name="currentDir"
- /
- --bndr777
- Content-Disposition: form-data; name="files"; filename="ipaddr.txt"
- Content-Type: text/plain
- xx.xx.xx.xx
- --bndr777--'''
- headers = {"Content-type": "multipart/form-data; boundary=bndr777","Content-Length": str(len(postbody)),"Accept": "text/html","Cookie": cooks,}
- conn.request("POST", "/filemanager/", postbody, headers)
- r1 = conn.getresponse()
- print r1.status, r1.reason
- data1 = r1.read()
- print("\n\n")
- print data1
- conn.close()
хтмл форма впридачу:
<form id="b-far-upload" action="/filemanager/" method="POST" enctype="multipart/form-data">
<div class="b-upload-file">
<input type="file" id="b-far-upload-file" name="file" value=""/>
</div>
<input type="hidden" name="action" value="upload"/>
<input type="hidden" name="currentDir" value="/"/>
</form>
<form id="b-far-upload" action="/filemanager/" method="POST" enctype="multipart/form-data">
<div class="b-upload-file">
<input type="file" id="b-far-upload-file" name="file" value=""/>
</div>
<input type="hidden" name="action" value="upload"/>
<input type="hidden" name="currentDir" value="/"/>
</form>