[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 实现的精致作品集图片导航效果
今天,我们要用 jQuery 来创建一个作品集图像的导航模板.我们的想法是,以分组的方式显示一组作品集,并通过二维的方式(水平/垂直)来浏览.任一箭头或当前图像下方的小盒子可以作为导航使用. 在线演示 ...
- 一款实用的viewer.js 图片相册
Viewer.js 是一款强大的图片相册插件,像SNS交友网站一般都会用到点击缩略图,弹出层大图片,而且弹出层有多个控制按钮,比如放大缩小.旋转.撤回等,底部有缩略图列表可切换. 支持移动设备触摸事件 ...
- 深入源码分析使用jQuery连续发起jsonp请求失败的原因
jQuery的 jsonp 大家应该是十分熟悉了.曾遇到过这样的需求1.希望请求几个相似的内容添加到页面2.请求的内容一定时间内是固定不变的,希望做个缓存. 于是脑子一拍写下了类似这样的代码 for( ...
- SAP 录屏BDC使用—实例
1) 输入TCode:SHDB进入BDC录制初始界面,该界面可以实现已创建BDC Session信息的查看.删除及锁定等操作 2) 单击工具栏 Newrecording 按钮创建一个新的BDC,系 ...
- tomcat通过socket连接MySQL,不再占用服务端口【linux】
MySQL连接方式的说明 http://icbm.iteye.com/blog/1840673 MySQL除了最常见的TCP连接方式外,还提供SOCKET(LINUX默认连接方式).PIPE和SHAR ...
- linux heartbeat v2/v3 的一点资料
http://linux-ha.org http://linux-ha.org/wiki/Pacemaker Heartbeat2 http://blog.taggesell.de/index.php ...
- LayoutInflater的infalte()
其中: resource:是布局文件ID root:是父ViewGroup对象, attachToRoot:是是否将“翻译”出来的View添加到上面的root中 root和attachToRoot是共 ...
- Android TextView走马灯效果
布局: <TextView android:id="@+id/myTextView" android:layout_width="match_parent" ...
- Object-c字符串操作
字符串操作: -(void) testString{ NSString *str1 = @"some string"; NSLog(@"%@", str1); ...
- IOS开发证书显示“此证书的签发者无效”解决方法
今天早上同事说咱们的证书无法使用了,显示“此证书的签发者无效”.一开始以为谁误操作了证书,查看后发现所有证书都无效了.查了会才发下原来是Apple Worldwide Developer Relati ...