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 kxbdMarquee 无缝滚动插件

    <marquee> 曾是 IE 下独有的一个走马灯效果的标签,其他浏览器并不兼容,于是出现了使用 JavaScript 来模拟该效果的插件. 版本: jQuery v1.3.2+ 在线实例 ...

  2. 【追寻javascript高手之路03】javascript对象大乱斗

    前言 昨天我们学习了下javascript中函数的参数与作用域的东东,现在回过头来看,其实都是与函数相关的,所以javascript中的函数仍然是王道,我们今天大概会发二篇或者三篇博客一起来巩固我们的 ...

  3. ASP.NET程序中常用的三十三种代码

    1. 打开新的窗口并传送参数: 传送参数: response.write("<script>window.open(’*.aspx?id="+this.DropDown ...

  4. ASP.NET多文件上传实例

    在Web应用程序开发中,避免不了要用到上传文件这个功能,但以前上传文件是个很麻烦的事,现在有了.NET,文件上传变得轻而易举.下面的这个例子实现了多文件上传功能.可以动态添加输入表单,上传的文件数量没 ...

  5. 自定义Sublime Text图标详细教程

    改造后的Sublime 先看看我改造后的神器图标,接下来你就知道下面是干什么的了 :               之前无意间在刘哇勇的博客上看到这么一篇文章,很好奇就尝试着更改Sublime Text ...

  6. iOS 正则 检测是否为手机号

    - (BOOL)validateMobile:(NSString *)mobileNum { NSString *regex = @"^1[3|5|7|8][0-9]\\d{8}$" ...

  7. 在ArcGIS中如何进行POI点抽稀

    对POI点添加权重等级类型信息.例如添加短整形字段Weight,并根据业务逻辑设置不同种类的POI的权重值,如分5类,0代表重要性最低的POI,4代表重要性最高. 在ArcMap的标注管理栏中,设置使 ...

  8. c#程序打包大全

    c#程序打包现在分为两种,一种是VS自带的打包方式,还有一种是第三方的打包方式,在VS2013里面是没有自带打包安装部署的,只有第三方的创建. 第三方打包方式很简单,百度Installshield下载 ...

  9. 小谈KVC中KeyPath的集合运算符

    由于知识点比较简单,这里不再陈述一大堆的原理,直入主题. KVC中的集合运算符有以下三类: 1.简单集合运算符:@avg.@sum.@max.@min.@count (只能用在集合对象中,对象属性必须 ...

  10. 【代码笔记】iOS-抽屉效果的实现

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...