This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://pymotw.com/2/threading/ | |
import threading | |
import logging | |
logging.basicConfig(level=logging.DEBUG, format='(%(threadName)-10s) %(message)s', ) | |
class MyThread(threading.Thread): | |
def __init__(self, num): | |
threading.Thread.__init__(self) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#http://ilab.cs.byu.edu/python/threadingmodule.html | |
import select | |
import socket | |
import sys | |
import threading | |
class Server: | |
def __init__(self): | |
self.host = 'localhost' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server_address = ('www.python.org', 80) | |
client_socket.connect(server_address) | |
request_header = 'GET / HTTP/1.0\r\nHost: www.python.org\r\n\r\n' | |
client_socket.send(request_header) | |
response = '' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get short tutorial here: http://www.pythonforbeginners.com/python-on-the-web/beautifulsoup-4-python/ | |
import urllib2 | |
from bs4 import BeautifulSoup | |
response = urllib2.urlopen('http://www.studiawan.com').read() | |
soup = BeautifulSoup(response) | |
print soup.title.string | |
print soup.get_text() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from HTMLParser import HTMLParser | |
import urllib2 | |
class MyHTMLParser(HTMLParser): | |
def handle_starttag(self, tag, attrs): | |
print "Encountered a start tag:", tag | |
def handle_endtag(self, tag): | |
print "Encountered an end tag :", tag | |
def handle_data(self, data): | |
print "Encountered some data :", data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import httplib | |
connection = httplib.HTTPConnection("its.ac.id") | |
connection.request("GET", "/index.php") | |
response = connection.getresponse() | |
header = response.getheaders() | |
print "Status:", response.status, response.reason |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pickle | |
# declare a list | |
mylist = [] | |
# assigning value to list | |
mylist.append('This is string') # string | |
mylist.append(5) # integer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import socket | |
import time | |
import sys | |
import pickle | |
def now(): | |
return time.asctime(time.localtime(time.time())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import socket | |
import pickle | |
import sys | |
# some definitions | |
SIZE = 1024 | |
# building socket |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://docs.python.org/2/library/xmlrpclib.html | |
import xmlrpclib | |
from SimpleXMLRPCServer import SimpleXMLRPCServer | |
def is_even(n): | |
return n%2 == 0 | |
server = SimpleXMLRPCServer(("localhost", 8000)) | |
print "Listening on port 8000..." |