[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() -> ...
随机推荐
- jQuery.buildFragment源码分析以及在构造jQuery对象的作用
这个方法在jQuery源码中比较靠后的位置出现,主要用于两处.1是构造jQuery对象的时候使用 2.是为DOM操作提供底层支持,这也就是为什么先学习它的原因.之前的随笔已经分析过jQuery的构造函 ...
- Hexo部署到GitHub出现spawn ENOENT的解决办法
最近用Hexo博客部署到GitHub时出现了这如下的错误: Error: spawn ENOENT at errnoException (child_process.js:980:11) at Pro ...
- 读取SHP的投影信息
from osgeo import ogr shp_driver = ogr.GetDriverByName('ESRI Shapefile') shp_dataset = shp_driver.Op ...
- 【思维导图】Fiddler学习笔记
最近在学习Fiddler这款工具,边学边画了如下的思维导图,可以方便自己对这款工具有一个全面的了解. 软件介绍(摘自百度百科):Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电 ...
- Android 图片的合成
本文实现在Android下图片的合成 布局设计比较简单: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r ...
- Android数据存储五种方式总结
本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用Cont ...
- sleep() 和 wait() 的区别
好多面经上都出现了,有必要好好熟悉一下 区别: 1.wait() 可以指定时间,也可以不指定(等五分钟你进来,或者是不叫你一直等着):sleep()必须指定时间(不能一睡不起) 2.wait()是Ob ...
- 【代码笔记】iOS-两个滚动条,上下都能滑动
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- OC中的Block的那些事
Block封装了一段代码,可以在任何时候执行 Block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值. 苹果官方建议尽量多用block.在多线程.异步任务.集合遍历.集合排序.动 ...
- location of the android sdk has not been setup in the preferences
点eclipse的android virtual device manager提示错误:error:location of the android sdk has not been setup in ...