[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() -> ...
随机推荐
- ASP.NET使用jQuery AJAX实现MD5加密实例
一个asp.net ajax例子,使用jquery,实现md5加密.在.NET 4.0,Visual Studio 2010上成功运行. 效果体验:http://tool.keleyi.com/t/m ...
- Sap 常用Function 说明
函数名 描述 SD_VBAP_READ_WITH_VBELN 根据销售订单读取表vbap中的信息EDIT_LINES 把READ_TEXT返回的LINES中的行按照TDFORMAT=“*”重新组织VI ...
- Microsoft Dynamics CRM MVP
MVP 一个响亮的名称, 不是 Kobe Bryant 不是 LeBron Raymone James 这是 Microsoft Dynamics CRM MVP 求证Link: http://mvp ...
- 关于内存泄露分析插件 MAT 的用法
关于内存泄露分析插件 MAT 的用法,建议大家有时间看一下,下面的文章 http://www.blogjava.net/rosen/archive/2010/05/21/321575.html htt ...
- 让background的图片不随着view的大小而改变
方法是在drawable文件中定义一个背景的xml文件. <?xml version="1.0" encoding="utf-8"?> <bi ...
- AFNetwork2.0在报错1016,3840的解决方法及一些感悟
最近在学习AFNetwork,非常好的网络框架,能节省很多时间.不过请求网络数据时报错1016,3840. 这两个错误网上解决方法很多,http://blog.csdn.net/huifeidexin ...
- 私有Pods封装个推SDK功能(解决方案)
一:运用场景 公司中同时有好几个APP在开发,而且每个APP都有使用到集成个推SDK来处理消息的功能,以前的做法是每个APP都去集成并在AppDelegate处理一些SDK的代码,包含个推基础配置.消 ...
- 我的第一节Android课
我的第一节安卓课程,今天非比寻常的一天,我开始了我程序猿之路的第一节安卓课程,安卓课程只是我的一个兴趣班,我的本专业是java开发,因为喜欢做一个属于自己的一个手机APP,就选多个一样技能,毕竟十八般 ...
- 【代码笔记】iOS-清除缓存有黑色背景(仿环球时报)
一,效果图. 二,代码. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UIAlertView * alterVi ...
- 【VLC-Android】LibVLC API简介(相当于VLC的MediaPlayer)
前言 学新东西API很重要,这里抛砖引玉整理了一下,欢迎反馈! 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over14 ...