Today I complete Socket Programming Assignment 1 Web Server

Here is the code:

#!/usr/bin/python2.7
# Program: Web Server
# Date: 2016-03-19
# Author: brant-ruan
# P.S.

from socket import *

serverPort = 12000

# Prepare a server socket
serverSocket = socket(AF_INET, SOCK_STREAM) # create socket
serverSocket.bind(('', serverPort)) # bind socket to specific port
serverSocket.listen(1) # listen connections

endFlag = '\x0d\x0a'
# Status
statusOK = 'HTTP/1.1 200 OK' + endFlag
statusErr = 'HTTP/1.1 404 Not Found' + endFlag
# HTTP header
header = 'Content-Type: text/html; charset=utf-8' + endFlag
while True:
    print 'Ready to serve...'
    (connectionSocket, addr) = serverSocket.accept() # create another connection socket
    try:
        message = connectionSocket.recv(1024) # receive messages
        filename = message.split()[1]   # fetch the filename client asks
        f = open(filename[1:], 'r')
        outputdata = f.read()
        f.close()
        connectionSocket.send(statusOK)  # 200 OK
        connectionSocket.send(header)   # header line (optional or not?)
        connectionSocket.send(endFlag)   # end of header
        for i in range(0, len(outputdata)):
            connectionSocket.send(outputdata[i])  # the whole file asked by client
        print 'OK'
        connectionSocket.close() # close the connection
    except IOError:
        print 'ERROR'
        connectionSocket.send(statusErr)
        connectionSocket.send(endFlag)
        connectionSocket.close()

serverSocket.close()

P.S.
I write a simple html file named test.html in the same directory where the webserver.py is located.
And we use DHCP to share only one external IP address.
Supposing that my internal IP address is 192.168.1.101, then I can input [http://192.168.1.101:12000/test.html] in the address label of browser to test.
However hosts in the external network can not communicate with your server process.
Here are two solutions (I think there at least these two solutions):

  1. Configure your router and map the 12000 port to your private IP(Your internal IP address 192.168.1....).
  2. Configure your router and specify your host as the DMZ host.

Whichever methods you use, now you can input [http://YOUR-EXTERNAL-IP-ADDRESS:PORT-ID/test.html] to your browser.


Here are some thoughts after finishing the experiment:

bingo!

[Top-Down Approach] Assignment 1: WebServer [Python]的更多相关文章

  1. _markupbase.py if not match: UnboundLocalError: local variable 'match' referenced before assignment,分析Python 库 html.parser 中存在的一个解析BUG

    BUG触发时的完整报错内容(本地无关路径用已经用 **** 隐去): **************\lib\site-packages\bs4\builder\_htmlparser.py:78: U ...

  2. Computer Networking: A Top Down Approach

    目录 Chapter 1: Computer Networks and the Internet 1. What is the Internet? 2. The Network Edge 3. The ...

  3. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  4. 002.Python数据类型

    一 python语言注释 就是对代码的解释, 方便大家阅读代码用的 1.1 注释的分类 (1)单行注释 # print 在python2.x print "1" # print 在 ...

  5. python deep copy and shallow copy

    Python中对于对象的赋值都是引用,而不是拷贝对象(Assignment statements in Python do not copy objects, they create bindings ...

  6. Python GUI with Tkinter (from youtube) 在youtube上能找到很多编程视频...

    Python GUI with Tkinter - 1 - Introduction以上链接是一个python tkinter视频系列的第一讲的链接.虽然英语不好,但是,程序还是看得懂的(照着做就可以 ...

  7. [Python 3.x 官方文档翻译]The Python Tutorial Python教程

    Python is an easy to learn, powerful programming language. It has efficient high-level data structur ...

  8. Comprehensive learning path – Data Science in Python深入学习路径-使用python数据中学习

    http://blog.csdn.net/pipisorry/article/details/44245575 关于怎么学习python,并将python用于数据科学.数据分析.机器学习中的一篇非常好 ...

  9. python字符串常用的方法解析

    这是本人在学习python过程中总结的一些关于字符串的常用的方法. 文中引用了python3.5版本内置的帮助文档,大致进行翻译,并添加了几个小实验. isalnum S.isalnum() -> ...

随机推荐

  1. jquery多次上传同一张图片

    $().reset(); wrap():$('p').wrap('div');===><div><p></p></div>; .closest() ...

  2. css权重

    1.行内样式,指的是html文档中定义的style <h1 style="color: #fff;">header</h1> 2.ID选择器 3.类,属性选 ...

  3. asp.net保存网上图片到服务器

    本文讲述的是根据一个图片的url地址,保存图片到asp.net服务器端的实现方法. 可以先了解这篇文章http://keleyi.com/a/bjac/0899dpgi.htm,以更好的理解本文. 首 ...

  4. [deviceone开发]-do_Dialog的基本使用示例

    一.简介 我们平常使用do_Notification的alert或者confirm都是比较简单弹窗. 更为复杂和个性化的弹窗需要用到do_Dialog, 它可以弹出一个自定义的窗口,窗口里的内容是你自 ...

  5. C#关于word文档的书签替换操作

    public void Get_Word(string gjbh) { try { DataSet ds = OperaterBase.GetDsBySql("select diffTabl ...

  6. js基本算法:冒泡排序,二分查找

    知识扩充: 时间复杂度:算法的时间复杂度是一个函数,描述了算法的运行时间.时间复杂度越低,效率越高. 自我理解:一个算法,运行了几次时间复杂度就为多少,如运行了n次,则时间复杂度为O(n). 1.冒泡 ...

  7. iOS 三方库fmdb 的使用

    使用fmdb 做本地数据的存储非常方便, 1. github 上搜索fmdb 下载压缩包 导入到工程中 (如果你的mac 有cocoapod 也可以直接通过cocoapod 添加) 2. 以下代码是通 ...

  8. iOS开发之图片分辨率与像素对齐

    像素对齐的概念 在iOS中,有一个概念叫做像素对齐,如果像素不对齐,那么在GPU渲染时,需要进行插值计算,这个插值计算的过程会有性能损耗. 在模拟器上,有一个选项可以把像素不对齐的部分显示出来.  ...

  9. CoreDataManager-OC版-兼容iOS10以前的版本

    头文件: #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> /** CoreData管理器 */ ...

  10. zookeeper入门讲解事例

    zookeeper使用和原理探究(一) zookeeper介绍zookeeper是一个为分布式应用提供一致性服务的软件,它是开源的Hadoop项目中的一个子项目,并且根据google发表的<Th ...