[Top-Down Approach] Assignment 1: WebServer [Python]
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):
- Configure your router and map the 12000 port to your private IP(Your internal IP address 192.168.1....).
- 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]的更多相关文章
- _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 ...
- Computer Networking: A Top Down Approach
目录 Chapter 1: Computer Networks and the Internet 1. What is the Internet? 2. The Network Edge 3. The ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- 002.Python数据类型
一 python语言注释 就是对代码的解释, 方便大家阅读代码用的 1.1 注释的分类 (1)单行注释 # print 在python2.x print "1" # print 在 ...
- python deep copy and shallow copy
Python中对于对象的赋值都是引用,而不是拷贝对象(Assignment statements in Python do not copy objects, they create bindings ...
- Python GUI with Tkinter (from youtube) 在youtube上能找到很多编程视频...
Python GUI with Tkinter - 1 - Introduction以上链接是一个python tkinter视频系列的第一讲的链接.虽然英语不好,但是,程序还是看得懂的(照着做就可以 ...
- [Python 3.x 官方文档翻译]The Python Tutorial Python教程
Python is an easy to learn, powerful programming language. It has efficient high-level data structur ...
- Comprehensive learning path – Data Science in Python深入学习路径-使用python数据中学习
http://blog.csdn.net/pipisorry/article/details/44245575 关于怎么学习python,并将python用于数据科学.数据分析.机器学习中的一篇非常好 ...
- python字符串常用的方法解析
这是本人在学习python过程中总结的一些关于字符串的常用的方法. 文中引用了python3.5版本内置的帮助文档,大致进行翻译,并添加了几个小实验. isalnum S.isalnum() -> ...
随机推荐
- JSCapture – 基于 HTML5 实现的屏幕捕捉库
JSCapture 是用纯 JavaScript 和 HTML5 实现的屏幕捕捉库.它可以让从您的浏览器中截图和记录在桌面的视频.JSCapture 使用 getUserMedia 来实现屏幕捕获.目 ...
- 【HTML5】<datalist>标签和<select>标签的比较
<select>标签: 注:该标签定义了下拉列表的实现 <select name = "location"> <option value = &quo ...
- ArcGisServer根据最大最小坐标换算瓦片行列号
1.前言 在上一节中我们知道了屏幕上一像素等于实际中多少单位长度(米或经纬度)的换算方法,而知道这个原理后,接下来我们要怎么用它呢?它和我们前端显示地图有什么关联呢?这一节,我会尽量详细的将这两个问题 ...
- centos初始配置
修改语言环境 [root@oracledb ~]# sudo vim /etc/sysconfig/i18n 将将zh_CH修改为"en_US.UTF-8" 搭建yum本地源 参考 ...
- GET/POST请求(NSURLSession)
步骤 使用NSURLSession创建task,然后执行task Task a.NSURLSessionTask是一个抽象类,本身不能使用,只能使用它的子类 b.NSURLSessionDataTas ...
- linux下安装jdk+tomcat+eclipse+mysql
我的环境:主机是win7的,虚拟机是VWare Workstation 6.0 ,linux系统为Red Hat Enterprise Linux 5 64位 各软件版本:jdk是jdk-6u ...
- 理解并自定义HttpHandler
前言 之前从网上找了几篇讲解如何自定义HttpHandler的文章,依葫芦画瓢却一直没成功过.经过上一篇<asp.net管道模型(管线模型)之一发不可收拾>的总结,对管道模型和请求/响应过 ...
- MongoDB-集群搭建
前言 搭建一个MongoDB的集群,这个环境只是内网的一个测试环境,分片没有使用副本集,配置并分配好端口后,开启集群的身份验证功能,在开启集群权限时,有些注意事项,在搭建过程中会着重标出. 一.集群规 ...
- 面试问题-使用Java线程做数学运算
这是一个展示如何使用join()方法的例子. 问题: 使用Java多线程计算表达式1*2/(1+2)的值. 解决方案: 使用一个线程做加法运算,另一个线程做乘法运算,还有一个主线程main做除法运算. ...
- Sublime更换默认字体的方法
Sublime是一款很不错的编辑器,不过默认安装后的字体却不尽人意,并且Sublime竟然连个完整的设置页面都没有(直接让你编辑配置文件).于是很多人对这字体就忍气吞声了.其实只要添加一行代码就可以完 ...