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. 如何在Visual Studio中开发自己的代码生成器插件

     Visual Studio是美国微软公司开发的一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等,且所写的目标代码适用于微 ...

  2. 深入理解javascript(一)

    此段文章摘自大叔的博客: 此文的目的是书写可维护的javascript代码. 最小的全局变量: JavaScript通过函数管理作用域.在函数内部声明的变量只在这个函数内部,函数外面不可用.另一方面, ...

  3. 关于sharepoint2013的SPUtility.GetGenericSetupPath()方法过期解决办法

    有个时候需要读取layouts下的xml文件,因此需要知道路径,以前在SP2010用的SPUtility.GetGenericSetupPath()方法获取.现在SP2013提示过期否决 看2个结构分 ...

  4. java 驼峰字符和下划线字符相互转换工具类

    public static final char UNDERLINE='_'; public static String camelToUnderline(String param){ if (par ...

  5. switch-枚举

    在swift中,如果switch的枚举,可以不写default,因为系统知道有多少种情况,如果不是枚举,必须要写default enum WBComposeToolBarButtonType:Int ...

  6. IOS开发——02_第一个类的创建、声明…

    在OC中,一般用2个文件来描述一个类: 1..h:类的声明文件,用于声明成员变量.方法.类的声明使用关键字@interface和@end. 注:.h中只是用做方法声明,并不进行实现.什么叫声明呢?简单 ...

  7. Android微信登陆

    前言 分享到微信朋友圈的功能早已经有了,但微信登录推出并不久,文档写的也并不是很清楚,这里记录分享一下. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.co ...

  8. android 数据存储Ⅰ

    本章讲述在Android开发中,简单的数据存储.涉及知识主要是SharedPreferences,及多页面切换ViewPager. 1.功能需求 做一个小应用.启动的时候有左右引导图.只有第一次启动时 ...

  9. 深入理解Objective-C Runtime

    一.简介 主要特点: 在OC语言中,函数的调用是属于动态调用的,编译阶段并不确定要调用的函数,在真正的运行时才会根据函数名查找要调用哪个函数. 而在C语言中,函数的调用是在编译阶段就已经确定要调用哪个 ...

  10. mac 终端 常用指令

    开始正式研究ios 应用开发,由于是从C开始学起,所以学习下常用的mac终端指令,方便后续常用操作. mac 终端 常用指令: 1.ls指令 用途:列出文件 常用参数 -w 以简洁的形式列出所有文件和 ...