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. require.js的一些了解

    想了解一下的可以轻轻点开链接,想了解的可以点开链接,http://mp.weixin.qq.com/s?__biz=MzIxMTQyNzExNw==&mid=100000042&idx ...

  2. [deviceone开发]-do_QRCode的简单示例

    一.简介 do_QRCode组件可以用来生成二维码,识别二维码图片文件,这个示例直观的展示组件基本的使用方式. 二.效果图 三.相关下载 https://github.com/do-project/c ...

  3. [deviceone开发]-打开新页动画效果

    一.简介 do_App的openPage支持16种过场动画,这个示例直观的展示16种动画的效果.适合初学者. 二.效果图 三.相关下载 https://github.com/do-project/co ...

  4. JavaScript实战-菜单特效

    以下是我自己用原生JS写的各种菜单特效,虽然网上一搜一大堆,但我还是喜欢自己来写一写! 这是上一篇:JavaScript实战(带收放动画效果的导航菜单) 下面是经过优化后的完整代码,优化了CSS样式. ...

  5. swift学习笔记之-属性

    //属性 import UIKit //属性(Properties)详解 /* 存储属性(Stored Properties):类.结构体中,不能在枚举中 计算属性(Computed Properti ...

  6. ArcGIS JS 学习笔记2 实现仿百度的拖拽画圆

    一.前言 吐槽一下,百度在国内除了百度地图是良心产品外,其他的真的不敢恭维.在上一篇笔记里,我已经实现了自定义的地图测量模块.在百度地图里面(其他地图)都有一个周边搜索的功能,拖拽画一个圆,然后以圆半 ...

  7. sharepoint2010如何本地化WebPart的Category、WebDisplayName 和 WebDescription 属性

    在项目中经常需要实现多语言其中包括webpart的属性也需要.那么如何实现呢? 首先需要资源文件,利用资源文件实现语言的翻译,如下图: 创建好资源后,下面我们来实现webpart属性的多语言.方法代码 ...

  8. 在Powershell ISE中添加sharepoint的智能提示,Enable SharePoint PowerShell Commandlets in the PowerShell ISE

    Powershell ISE在默认状态下有一个不好的地方就是不会显示关于SharePoint的一些智能提示,例如你写一个"get-"后面提示的选项里没有sp开头的一些对象.于是找了 ...

  9. Day Tips:分布式缓存的删除和重建

    遇到cacheHostInfo is null 错误时,必须将这台服务器上的实例删除重新创建 $instanceName ="SPDistributedCacheService Name=A ...

  10. 你真的了解UIResponder吗?

    1:首先查看一下关于UIResponder的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIResponder : NSObject //响应链中负责传递事件的 ...