python:internet
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python:internet [2014/08/08 15:10] – [Get Cookie Information] admin | python:internet [2022/10/29 16:15] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 346: | Line 346: | ||
| Accept: text/plain | Accept: text/plain | ||
| User-Agent: Python-urllib/ | User-Agent: Python-urllib/ | ||
| + | </ | ||
| + | ==== Get Cookie Information ==== | ||
| + | <code python> | ||
| + | import urllib, urllib2 | ||
| + | import cookielib | ||
| + | |||
| + | loginurl = ' | ||
| + | params = urllib.urlencode({' | ||
| + | headers = {" | ||
| + | " | ||
| + | req = urllib2.Request(loginurl, | ||
| + | |||
| + | cj = cookielib.CookieJar() | ||
| + | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | ||
| + | response = opener.open(req) | ||
| + | |||
| + | for cookie in cj: | ||
| + | print cookie.name, | ||
| + | data = response.read() | ||
| + | with open(' | ||
| + | f.write(data) | ||
| + | </ | ||
| + | currency VND | ||
| + | language vn | ||
| + | PHPSESSID 01eg0u7uf5bm3r01h6pnrv3q33 | ||
| + | </ | ||
| + | ==== Post multipart form data ==== | ||
| + | === post with encode_multipart_formdata === | ||
| + | Post with httplib< | ||
| + | import httplib, mimetypes | ||
| + | |||
| + | def post_multipart(host, | ||
| + | """ | ||
| + | Post fields and files to an http host as multipart/ | ||
| + | fields is a sequence of (name, value) elements for regular form fields. | ||
| + | files is a sequence of (name, filename, value) elements for data to be uploaded as files | ||
| + | Return the server' | ||
| + | """ | ||
| + | content_type, | ||
| + | h = httplib.HTTP(host) | ||
| + | h.putrequest(' | ||
| + | h.putheader(' | ||
| + | h.putheader(' | ||
| + | h.endheaders() | ||
| + | h.send(body) | ||
| + | errcode, errmsg, headers = h.getreply() | ||
| + | return h.file.read() | ||
| + | |||
| + | def encode_multipart_formdata(fields, | ||
| + | """ | ||
| + | fields is a sequence of (name, value) elements for regular form fields. | ||
| + | files is a sequence of (name, filename, value) elements for data to be uploaded as files | ||
| + | Return (content_type, | ||
| + | """ | ||
| + | BOUNDARY = ' | ||
| + | CRLF = ' | ||
| + | L = [] | ||
| + | for (key, value) in fields: | ||
| + | L.append(' | ||
| + | L.append(' | ||
| + | L.append('' | ||
| + | L.append(value) | ||
| + | for (key, filename, value) in files: | ||
| + | L.append(' | ||
| + | L.append(' | ||
| + | L.append(' | ||
| + | L.append('' | ||
| + | L.append(value) | ||
| + | L.append(' | ||
| + | L.append('' | ||
| + | body = CRLF.join(L) | ||
| + | content_type = ' | ||
| + | return content_type, | ||
| + | |||
| + | def get_content_type(filename): | ||
| + | return mimetypes.guess_type(filename)[0] or ' | ||
| + | </ | ||
| + | === fix encode_multipart_formdata for posting binary file === | ||
| + | <code python> | ||
| + | def encode_multipart_formdata(fields, | ||
| + | """ | ||
| + | fields is a sequence of (name, value) elements for regular form fields. | ||
| + | files is a sequence of (name, filename, value) elements for data to be uploaded as files | ||
| + | Return (content_type, | ||
| + | """ | ||
| + | | ||
| + | buf = StringIO() | ||
| + | boundary = mimetools.choose_boundary() | ||
| + | for (key, value) in fields: | ||
| + | buf.write(' | ||
| + | buf.write(' | ||
| + | buf.write(' | ||
| + | for (key, filename, value) in files: | ||
| + | contenttype = mimetypes.guess_type(filename)[0] or ' | ||
| + | buf.write(' | ||
| + | buf.write(' | ||
| + | buf.write(' | ||
| + | buf.write(' | ||
| + | buf.write(' | ||
| + | buf = buf.getvalue() | ||
| + | content_type = ' | ||
| + | return content_type, | ||
| + | </ | ||
| + | === Post using MultipartPostHandler === | ||
| + | * Install:< | ||
| + | pip install MultipartPostHandler | ||
| + | </ | ||
| + | * example:< | ||
| + | import MultipartPostHandler, | ||
| + | |||
| + | cookies = cookielib.CookieJar() | ||
| + | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), | ||
| + | MultipartPostHandler.MultipartPostHandler) | ||
| + | params = { " | ||
| + | " | ||
| + | opener.open(" | ||
| </ | </ | ||
| ===== twisted internet ===== | ===== twisted internet ===== | ||
python/internet.1407510608.txt.gz · Last modified: (external edit)
