[Top-Down Approach]My First C/S Program [Python]
These days I was learning from Computer Networking --A Top-Down Approach by Kurose Ross.
I modified the C/S model socket program in Python in Chapter 2.
Here is the program's source code.
#!/usr/bin/python2.7
# UDP - Client
from socket import *
serverName = '192.168.1.105'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
while 1:
message = raw_input('my reply:')
clientSocket.sendto(message, (serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print 'his reply: ' + modifiedMessage
clientSocket.close()
#!/usr/bin/python2.7
# UDP - Server
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print 'The server is ready to receive'
while 1:
message, clientAddress = serverSocket.recvfrom(2048)
print message + str(clientAddress)
mymsg = raw_input("my reply:")
serverSocket.sendto(mymsg, clientAddress)
#!/usr/bin/python2.7
# TCP - Client
from socket import *
serverName = '192.168.1.105'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
while True:
clientSocket.connect((serverName, serverPort))
sentence = raw_input('input sentence: ')
clientSocket.send(sentence)
hisReply = clientSocket.recv(1024)
print 'From Server:', hisReply
clientSocket.close()
#!/usr/bin/python2.7
# TCP - Server
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(3)
print 'The server is ready to receive'
while True:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024)
print sentence + str(addr)
myreply = raw_input('input sentence:')
connectionSocket.send(myreply)
connectionSocket.close()
Bingo!
[Top-Down Approach]My First C/S Program [Python]的更多相关文章
- Computer Networking: A Top Down Approach
目录 Chapter 1: Computer Networks and the Internet 1. What is the Internet? 2. The Network Edge 3. The ...
- python调用top命令获得CPU利用率
1.python调用top命令获得CPU利用率 思路:通过python调用top命令获取cpu使用率 #python2代码 [root@zdops-server script]# cat cpu_lo ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
- Python框架、库以及软件资源汇总
转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- C 和 C++的 不同
转自: http://studytipsandtricks.blogspot.com/2012/05/15-most-important-differences-between-c.html Basi ...
- Code Complete阅读笔记(三)
2015-05-26 628 Code-Tuning Techniques ——Even though a particular technique generally represen ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- 04 Effective Go 高效的go语言 重点内容
Effective Go 高效的go语言 Introduction 介绍 Examples 例子 Formatting 格式 Commentary 评论 Names 名字 Package names ...
随机推荐
- 【CSS3】CSS3:border-image的详解和实例
border-image简介 border-image是CSS3中的新特性.目前几乎所有的主流浏览器都已经支持该特性,详情请移步border-image的兼容性. border-image属性及使用说 ...
- SharePoint 2013 配置基于AD的Form认证
前 言 配置SharePoint 2013基于AD的Form认证,主要有三步: 1. 修改管理中心的web.config: 2. 修改STS Application的web.config: 3. 修改 ...
- c#程序打包大全
c#程序打包现在分为两种,一种是VS自带的打包方式,还有一种是第三方的打包方式,在VS2013里面是没有自带打包安装部署的,只有第三方的创建. 第三方打包方式很简单,百度Installshield下载 ...
- setRequestedOrientation设置屏幕方向
void android.app.Activity.setRequestedOrientation(int requestedOrientation) 官方API解释: Change th ...
- Android 图片的合成
本文实现在Android下图片的合成 布局设计比较简单: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r ...
- 优化MySchool数据库(四)
关于“无法附件数据库”过程的遇到的问题: 1.数据文件本身,具有访问权限的限制 ---- 选中 数据库文件所在的文件夹---->右键菜单(属性)----> 安全 --->User用户 ...
- 【代码笔记】iOS-轮询弹出框
一,效果图. 二,工程图. 三,代码. RootViewController.m #import "RootViewController.h" //加入弹出框的头文件 #impor ...
- 【工具相关】iOS-Reveal的使用
一,首先下载Reveal工具. Reveal的下载地址:http://revealapp.com/download. 下载界面如下图所示,选择Download Trail蓝色按钮可以进行下载: 二,新 ...
- DB2LOOK命令提取数据库对象信息
提取复制数据库的DDL语句:db2look -d BCDLJS -e -o db2look.sql -a -a:导出所有用户的DDL-o: 定向结果到文件-d: 数据库名-e: 抽取复制数据库所需的 ...
- C#中方法的参数的四种类型
C#中方法的参数有四种类型: 1. 值参数类型 (不加任何修饰符,是默认的类型) 2. 引用型参数 (以ref 修饰符声明) 3. 输出型参数 (以out 修 ...