From 0f4fffd87ec52fcd1c5c8f0d8cfe733795be9a7f Mon Sep 17 00:00:00 2001 From: daviracvant Date: Sat, 11 Jan 2014 01:57:19 -0800 Subject: [PATCH 1/5] Echo server and client. --- assignments/session01/echo_client.py | 40 ++++++++--------- assignments/session01/echo_server.py | 66 +++++++++------------------- 2 files changed, 40 insertions(+), 66 deletions(-) diff --git a/assignments/session01/echo_client.py b/assignments/session01/echo_client.py index 61616c36..c1bd0669 100644 --- a/assignments/session01/echo_client.py +++ b/assignments/session01/echo_client.py @@ -4,32 +4,30 @@ def client(msg, log_buffer=sys.stderr): server_address = ('localhost', 10000) - # TODO: Replace the following line with your code which will instantiate - # a TCP socket with IPv4 Addressing, call the socket you make 'sock' - sock = None + # a TCP socket with IPv4 Addressing, call the socket you make 'sock' + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + # print the address that client will connect to. print >>log_buffer, 'connecting to {0} port {1}'.format(*server_address) - # TODO: connect your socket to the server here. - - # this try/finally block exists purely to allow us to close the socket - # when we are finished with it + # connect the socket to the server address. + sock.connect(server_address) + # this try/finally block exists purely to allow us to close the socket on complete. try: print >>log_buffer, 'sending "{0}"'.format(msg) - # TODO: send your message to the server here. - - # TODO: the server should be sending you back your message as a series - # of 16-byte chunks. You will want to log them as you receive - # each one. You will also need to check to make sure that - # you have received the entire message you sent __before__ - # closing the socket. - # - # Make sure that you log each chunk you receive. Use the print - # statement below to do it. (The tests expect this log format) - chunk = '' - print >>log_buffer, 'received "{0}"'.format(chunk) + # send the message to the server. + sock.send(msg) + # the server sent message as a series of 16-byte chunks. Print each message, and wait until receiving + # the entire message before closing the socket. + message_return = '' + while message_return != msg: + chunk = sock.recv(16) + if not chunk: + break + message_return += chunk + print >>log_buffer, 'received "{0}"'.format(chunk) finally: - # TODO: after you break out of the loop receiving echoed chunks from - # the server you will want to close your client socket. + # close the client server after receiving the entire message. print >>log_buffer, 'closing socket' + sock.close() if __name__ == '__main__': diff --git a/assignments/session01/echo_server.py b/assignments/session01/echo_server.py index 217380fb..aeae64ce 100644 --- a/assignments/session01/echo_server.py +++ b/assignments/session01/echo_server.py @@ -5,64 +5,40 @@ def server(log_buffer=sys.stderr): # set an address for our server address = ('127.0.0.1', 10000) - # TODO: Replace the following line with your code which will instantiate - # a TCP socket with IPv4 Addressing, call the socket you make 'sock' - sock = None - # TODO: Set an option to allow the socket address to be reused immediately - # see the end of http://docs.python.org/2/library/socket.html - + # a TCP socket with IPv4 Addressing, call the socket you make 'sock' + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + # log that we are building a server print >>log_buffer, "making a server on {0}:{1}".format(*address) - - # TODO: bind your new sock 'sock' to the address above and begin to listen - # for incoming connections - + + # for incoming connections + sock.bind(address) + sock.listen(1) try: # the outer loop controls the creation of new connection sockets. The # server will handle each incoming connection one at a time. while True: print >>log_buffer, 'waiting for a connection' - - # TODO: make a new socket when a client connects, call it 'conn', - # at the same time you should be able to get the address of - # the client so we can report it below. Replace the - # following line with your code. It is only here to prevent - # syntax errors - addr = ('bar', 'baz') + # accept the client connection, get the socket connected and the address. + connection, client_address = sock.accept() try: - print >>log_buffer, 'connection - {0}:{1}'.format(*addr) - + print >>log_buffer, 'connection - {0}:{1}'.format(*client_address) # the inner loop will receive messages sent by the client in - # buffers. When a complete message has been received, the - # loop will exit + # buffers and exit when a complete message has been received. while True: - # TODO: receive 16 bytes of data from the client. Store - # the data you receive as 'data'. Replace the - # following line with your code. It's only here as - # a placeholder to prevent an error in string - # formatting - data = '' + # receive 16 bytes of data from the client. Store the data you receive as 'data'. + data = connection.recv(16) + # if no data receive, exit the inner loop. + if not data: + break print >>log_buffer, 'received "{0}"'.format(data) - # TODO: you will need to check here to see if any data was - # received. If so, send the data you got back to - # the client. If not, exit the inner loop and wait - # for a new connection from a client - + connection.send(data) finally: - # TODO: When the inner loop exits, this 'finally' clause will - # be hit. Use that opportunity to close the socket you - # created above when a client connected. Replace the - # call to `pass` below, which is only there to prevent - # syntax problems - pass - + # close the connection. + connection.close() except KeyboardInterrupt: - # TODO: Use the python KeyboardIntterupt exception as a signal to - # close the server socket and exit from the server function. - # Replace the call to `pass` below, which is only there to - # prevent syntax problems - pass - + # close the server on key interrupt. + sock.close() if __name__ == '__main__': server() From 59cdc9ee3a8c7a7f8f244fd030e6edd33cabd84d Mon Sep 17 00:00:00 2001 From: daviracvant Date: Fri, 17 Jan 2014 01:14:13 -0800 Subject: [PATCH 2/5] Rithy Chhen Assignment 2: http_server --- assignments/session02/http_server.py | 42 ++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/assignments/session02/http_server.py b/assignments/session02/http_server.py index 12cbfeba..6f41b6df 100644 --- a/assignments/session02/http_server.py +++ b/assignments/session02/http_server.py @@ -1,14 +1,16 @@ import socket import sys +import os +import mimetypes -def response_ok(): +def response_ok(body='', mimetype='text/plain'): """returns a basic HTTP response""" resp = [] resp.append("HTTP/1.1 200 OK") - resp.append("Content-Type: text/plain") + resp.append("Content-Type: "+mimetype) resp.append("") - resp.append("this is a pretty minimal response") + resp.append(body) return "\r\n".join(resp) @@ -19,13 +21,38 @@ def response_method_not_allowed(): resp.append("") return "\r\n".join(resp) +def response_not_found(): + """returns a 404 method not found""" + resp = [] + resp.append("HTTP/1.1 404 Not Found") + resp.append("") + return "\r\n".join(resp) def parse_request(request): + """parse request would return an uri""" first_line = request.split("\r\n", 1)[0] method, uri, protocol = first_line.split() if method != "GET": raise NotImplementedError("We only accept GET") print >>sys.stderr, 'request is okay' + return uri + +def resolve_uri(uri): + """resolving uri; checking to see if it exist, is a directory or file.""" + root = 'webroot' + path = root + uri + if not os.path.exists(path): + raise ValueError("uri doesn't exist") + else: + if os.path.isdir(path): + list = os.listdir(path) + mimetype = "text/plain" + return " ".join(list), mimetype + elif os.path.isfile(root + uri): + body = open(path, 'rb').read() + extension = os.path.splitext(path)[1] + mimetype = mimetypes.types_map[extension] + return body, mimetype def server(): @@ -50,11 +77,16 @@ def server(): break try: - parse_request(request) + uri = parse_request(request) except NotImplementedError: response = response_method_not_allowed() else: - response = response_ok() + try: + body, mimetype = resolve_uri(uri) + except ValueError: + response = response_not_found() + else: + response = response_ok(body, mimetype) print >>sys.stderr, 'sending response' conn.sendall(response) From 83e43f660f66bfdff3b75e667719dcb099d912a8 Mon Sep 17 00:00:00 2001 From: daviracvant Date: Sat, 25 Jan 2014 22:29:01 -0800 Subject: [PATCH 3/5] ignored --- .gitignore | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d04dac0b..c5c5181c 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,9 @@ svn-commit.tmp bin build include -lib \ No newline at end of file +lib +.idea/scopes/scope_settings.xml + +.idea/training.python_web.iml + +.idea/workspace.xml From 46f6dc4d0cb88bc98c91e0baed580f81d813cecc Mon Sep 17 00:00:00 2001 From: daviracvant Date: Sun, 26 Jan 2014 02:55:07 -0800 Subject: [PATCH 4/5] Rithy Chhen: Assignment 3 --- assignments/session03/README.txt | 27 ++++++++++ assignments/session03/youtube.py | 88 ++++++++++++++++++++++++++++++++ assignments/session03/yt.py | 42 +++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 assignments/session03/README.txt create mode 100644 assignments/session03/youtube.py create mode 100644 assignments/session03/yt.py diff --git a/assignments/session03/README.txt b/assignments/session03/README.txt new file mode 100644 index 00000000..ff625d98 --- /dev/null +++ b/assignments/session03/README.txt @@ -0,0 +1,27 @@ +****************************************************************** + Session:3 Assignments / Rithy Chhen + yt.py - Youtube Mashup +***************************************************************** + +Brief Description : + I am a big fan of youtube, therefore I decided to do this assignment on YouTube API. My program will do a search for + the most popular video on youtube by regions. Each video is tag with duration, uploader, published date and number of view. + +Think you should know: + 1) YouTube API. + The YouTube Data API allows me to grab the data from youtube without authenticating. + 2) Knowing what regions is supported by Youtube Data API: + I spend a lot of time searching for supported region by youtube. + I listed over 20 different regions. + 3) In order to run yt.py, couple modules need to be installed: + - requests + - BeautifulSoup + - Codecs + +Source Code: + When everything is ready. You can simply run python.py. If would take a couple of seconds to complete because I have over 20 different regions + listed. + +Output Example: + Once the program is completed, a new file name most_popular.html should be created and placed in the session03 folder. Open most_popular.html + with your favorite browser, you should see a list of popular video separated by regions. \ No newline at end of file diff --git a/assignments/session03/youtube.py b/assignments/session03/youtube.py new file mode 100644 index 00000000..70f8cf82 --- /dev/null +++ b/assignments/session03/youtube.py @@ -0,0 +1,88 @@ +__author__ = 'rithychhen' + +import requests +import codecs +from bs4 import BeautifulSoup + + +#A youtube api call to do the api call and parsing the return cotent. +class Youtube: + + #constructing the Youtube API object, default region to US. + def __init__(self, region_id='US'): + self.region_id = region_id + self.most_popular_api = 'http://gdata.youtube.com/feeds/api/standardfeeds/%s/most_popular?v=2&max-results=11' + + #a simple function to set the regions. + def set_region_id(self, region_id): + self.region_id = region_id + + #a simple get request to youtube to get the most popular video by region. + #using the request module to get a get request to youtube. + #return value are status and content. + def get_most_popular_by_region(self): + api_string = self.most_popular_api % self.region_id + response = requests.get(api_string) + return response.status_code, response.content + + #parse the content from youtube using BeautifulSoup. + def parse_content(self, content): + parsed_content = [] + content = BeautifulSoup(content) + content.prettify() + for entry in content.find_all("entry"): + parsed_content.append(self.gen_entry(entry)) + return parsed_content + + #generate a single video entry. + def gen_entry(self, entry): + entry_parsed = dict() + title = entry.title.string + video_id = entry.content.find("yt:videoid") + link = "http://www.youtube.com/watch?v=%s" % video_id.string + entry_parsed['title'] = "%s" % (link, title) + entry_parsed['duration'] = entry.content.find("yt:duration").get("seconds") + author = entry.content.author.find('name') + entry_parsed['Uploader'] = author.string + entry_parsed['Published on'] = entry.published.string + statistic = entry.content.find("yt:statistics") + entry_parsed["View count"] = statistic.get("viewcount") + #entry_parsed["avorite count"]= statistic.get("favoritecount") + entry_parsed["Dislikes"] = statistic.get("numdislikes") + entry_parsed["Likes"] = statistic.get("numlikes") + return entry_parsed + + #write the result into html. + def write_to_html(self, result): + html = "

Most popular videos by region

" + for item in result: + #print item + for name, content in item.iteritems(): + #print name + html += "

%s

" % name + if isinstance(content, str): + html += content + '
' + else: + html += "
    " + for sub_item in content: + temp_string = '' + for key, value in sub_item.iteritems(): + if key == 'title': + html += "
  • %s" % value + elif key == 'duration': + html += "%s seconds
    " % value + elif key == 'Dislikes' or key == "Likes": + continue + else: + temp_string += "%s : %s | " % (key, value) + html += temp_string + '
  • ' + html += "

" + html += "" + self.put_file(html) + + #write html to a file. + def put_file(self, html): + with codecs.open("most_popular.html", "w", "utf-8-sig") as f: + f.write(html) + f.close() + diff --git a/assignments/session03/yt.py b/assignments/session03/yt.py new file mode 100644 index 00000000..597c5093 --- /dev/null +++ b/assignments/session03/yt.py @@ -0,0 +1,42 @@ +__author__ = 'rithychhen' + +#import the YoutubeAPI class. +from youtube import Youtube + +#function to list the region that I am interesting in. +def generate_supported_region_ids(): + regions_ids = {'Argentina': 'AR', 'Australia': 'AU', 'Brazil': 'BR', 'Canada': 'CA', + 'Egypt': 'EG', 'France': 'FR', 'Germany': 'DE', 'Great Britain': 'GB', + 'Hong Kong': 'HK', 'India': 'IN', 'Ireland': 'IE', 'Italy': 'IT', + 'Japan': 'JP', 'Malaysia': 'MY', 'Mexico': 'MX', 'Netherlands': 'NL', + 'New Zealand': 'NZ', 'Russia': 'RU', 'Saudi Arabia': 'SA', 'Singapore': 'SG', + 'South Africa': 'ZA', 'South Korea': 'KR', 'Spain': 'ES', 'Sweden': 'SE', + 'Switzerland': 'CH', 'Taiwan': 'TW', 'United States': 'US'} + return regions_ids + +#main +if __name__ == '__main__': + #instantiate the youtubapi class. This would default the region to US and api string. + youtube = Youtube() + #get a dictionary of regions ids. + regions_ids = generate_supported_region_ids() + #a list to store the result. + result = [] + #go through all regions ids. + for name, region_id in regions_ids.iteritems(): + + temp_result = dict() + #first set the region to region id. + youtube.set_region_id(region_id) + #then do an api to youtube to get the top 10. + status, body = youtube.get_most_popular_by_region() + #if response from youtube is not okay then store the error. + if status != 200: + temp_result[name] = 'Unable to get data for %s' % name + else: + #okay response. parse youtube content and store to the list. + temp_result[name] = youtube.parse_content(body) + #finally add the dictionary to the list. + result.append(temp_result) + #lastly write the list to html file. + youtube.write_to_html(result) From e1c20d63353071e8e184ef0301e0de39d8a0f668 Mon Sep 17 00:00:00 2001 From: daviracvant Date: Sun, 26 Jan 2014 02:58:22 -0800 Subject: [PATCH 5/5] yt.py --- assignments/session03/README.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/session03/README.txt b/assignments/session03/README.txt index ff625d98..906130fb 100644 --- a/assignments/session03/README.txt +++ b/assignments/session03/README.txt @@ -19,7 +19,7 @@ Think you should know: - Codecs Source Code: - When everything is ready. You can simply run python.py. If would take a couple of seconds to complete because I have over 20 different regions + When everything is ready. You can simply run python yt.py. If would take a couple of seconds to complete because I have over 20 different regions listed. Output Example: