[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() -> ...
随机推荐
- 3种不同的ContextMenu右键菜单演示
简单使用的右键菜单,希望能帮助大家.下面是截图和实例代码 实例预览 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...
- MySQL支持的数据类型
1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节 范围(-128~127) smallint(m) 2个字节 范围(-32768~32767) mediumint(m) 3 ...
- jQuery的deferred对象使用详解——实现ajax线性请求数据
最近遇到一个ajax请求数据的问题 ,就是想要请求3个不同的接口,然后请求完毕后对数据进行操作,主要问题就是不知道这3个请求誰先返回来,或者是在进行操作的时候不能保证数据都已经回来,首先想到能完成的就 ...
- (转)高性能JavaScript:加载和运行(动态加载JS代码)
浏览器是如何加载JS的 当浏览器遇到一个<script>标签时,浏览器首先根据标签src属性下载JavaScript代码,然后运行JavaScript代码,继而继续解析和翻译页面.如果需要 ...
- overflow 属性
写在前面的话: 2016年5月4日青年节,作为一名正青春的学生党,开始了博客生涯,励志做个勤奋上进的好青年.幻想着毕业后月薪W+ .走上人生巅峰的职场生活...... 然而 然而 然而 ,自制力有限的 ...
- flume 集群安装
./pssh -h ./host/all.txt -P mkdir /usr/local/app ./pssh -h ./host/all.txt -P tar zxf /usr/local/soft ...
- Xcode中的常用快捷键
新建项目 com + shift +N 新建文件 com + N 偏好设置 通用 com + , 跳到指定行 com + L 当前行加断点 com + \ 移动编辑区最上方 ...
- 关于Android Force Close 出现的原因 以及解决方法
一.原因: forceclose,意为强行关闭,当前应用程序发生了冲突. NullPointExection(空指针),IndexOutOfBoundsException(下标越界),就连Androi ...
- 【代码笔记】iOS-离线地图
一,效果图. 二,工程图. 三,代码. ViewController.h #import <UIKit/UIKit.h> #import <CoreLocation/CoreLoca ...
- Photo Shop 修改、维护
调整画布大小 要继续放更多的图片? 更改画布大小 移动图标 若图标为独立图层,则用移动工具拖动即可 若图层为非独立图层 - 用选区工具选中图标区域 - 用移动工具拖动图标 如果要拆分同一图层下的两个图 ...