Table of Contents

Python Internet

Python Socket

refer: https://docs.python.org/2/library/socket.html

Echo Server and Client

Simple Echo Server and Client

Custom Echo Server for multiple client

The above example only service for 1 client, after the server receive connection and send data to client, the echoserver program will end. The below example will service for multiple clients

Connect to google with socket and receive data

#Socket client example in python
 
import socket   #for sockets
import sys  #for exit
 
#create an INET, STREAMing socket
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print 'Failed to create socket'
    sys.exit()
 
print 'Socket Created'
 
host = 'www.google.com';
port = 80;
 
try:
    remote_ip = socket.gethostbyname( host )
 
except socket.gaierror:
    #could not resolve
    print 'Hostname could not be resolved. Exiting'
    sys.exit()
 
#Connect to remote server
s.connect((remote_ip , port))
 
print 'Socket Connected to ' + host + ' on ip ' + remote_ip
 
#Send some data to remote server
message = "GET / HTTP/1.1\r\n\r\n"
 
try :
    #Set the whole string
    s.sendall(message)
except socket.error:
    #Send failed
    print 'Send failed'
    sys.exit()
 
print 'Message send successfully'
 
#Now receive data
reply = s.recv(4096)
 
print reply

output

Socket Created
Socket Connected to www.google.com on ip 125.235.30.157
Message send successfully
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.com.vn/?gfe_rd=cr&ei=Cd_gU72uKIiK8Qe6koDICQ
Content-Length: 262
Date: Tue, 05 Aug 2014 13:41:29 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.vn/?gfe_rd=cr&amp;ei=Cd_gU72uKIiK8Qe6koDICQ">here</A>.
</BODY></HTML>

httplib and urllib2(with addinfourl)

urllib2.urlopen return addinfourl and debug send data in request

simple request(GET)

simple post

urllib2.build_opener(replace urlopen) and post with cookies

import urllib, urllib2
import cookielib
 
loginurl = 'http://dacsan.babies.vn/index.php?route=account/login'
params = urllib.urlencode({'email': '[email protected]', 'password':'8941362', 'redirect':'http://dacsan.babies.vn/index.php?route=account/account'})
headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
req = urllib2.Request(loginurl, data = params, headers = headers)
 
cj = cookielib.CookieJar() 
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
response = opener.open(req)
 
for cookie in cj:
    print cookie.name, cookie.value
data = response.read()
with open('loginresult.html', 'wb') as f:
    f.write(data)

output:

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, selector, fields, files):
    """
    Post fields and files to an http host as multipart/form-data.
    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's response page.
    """
    content_type, body = encode_multipart_formdata(fields, files)
    h = httplib.HTTP(host)
    h.putrequest('POST', selector)
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body)))
    h.endheaders()
    h.send(body)
    errcode, errmsg, headers = h.getreply()
    return h.file.read()
 
def encode_multipart_formdata(fields, files):
    """
    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, body) ready for httplib.HTTP instance
    """
    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for (key, filename, value) in files:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        L.append('Content-Type: %s' % get_content_type(filename))
        L.append('')
        L.append(value)
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body
 
def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'

fix encode_multipart_formdata for posting binary file

def encode_multipart_formdata(fields, files):
    """
    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, body) ready for httplib.HTTP instance
    """    
 
    buf = StringIO()
    boundary = mimetools.choose_boundary()            
    for (key, value) in fields:
        buf.write('--%s\r\n' % boundary)
        buf.write('Content-Disposition: form-data; name="%s"' % key)
        buf.write('\r\n\r\n' + value + '\r\n')
    for (key, filename, value) in files:
        contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
        buf.write('--%s\r\n' % boundary)
        buf.write('Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename))
        buf.write('Content-Type: %s\r\n' % contenttype)
        buf.write('\r\n' + value + '\r\n')
    buf.write('--' + boundary + '--\r\n\r\n')
    buf = buf.getvalue()
    content_type = 'multipart/form-data; boundary=%s' % boundary
    return content_type, buf

Post using MultipartPostHandler

twisted internet

Simple Request

from sys import argv
from pprint import pformat
 
from twisted.internet.task import react
from twisted.web.client import Agent, readBody
from twisted.web.http_headers import Headers
 
 
def cbRequest(response):
    print 'Response version:', response.version
    print 'Response code:', response.code
    print 'Response phrase:', response.phrase
    print 'Response headers:'
    print pformat(list(response.headers.getAllRawHeaders()))
    d = readBody(response)
    d.addCallback(cbBody)
    return d
 
def cbBody(body):
    print 'Response body:'
    print body
 
def main(reactor, url=b"http://www.google.com/"):
    agent = Agent(reactor)
    d = agent.request(
        'GET', url,
        Headers({'User-Agent': ['Twisted Web Client Example']}),
        None)
    d.addCallback(cbRequest)
    return d
 
react(main, argv[1:])