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]的更多相关文章

  1. Computer Networking: A Top Down Approach

    目录 Chapter 1: Computer Networks and the Internet 1. What is the Internet? 2. The Network Edge 3. The ...

  2. python调用top命令获得CPU利用率

    1.python调用top命令获得CPU利用率 思路:通过python调用top命令获取cpu使用率 #python2代码 [root@zdops-server script]# cat cpu_lo ...

  3. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

  4. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  5. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  6. C 和 C++的 不同

    转自: http://studytipsandtricks.blogspot.com/2012/05/15-most-important-differences-between-c.html Basi ...

  7. Code Complete阅读笔记(三)

    2015-05-26   628   Code-Tuning Techniques    ——Even though a particular technique generally represen ...

  8. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  9. 04 Effective Go 高效的go语言 重点内容

    Effective Go  高效的go语言 Introduction 介绍 Examples 例子 Formatting 格式 Commentary 评论 Names 名字 Package names ...

随机推荐

  1. 网页引用本地电脑的字体 css设置浏览器会不显示的解决办法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. js中如何获取纯正的undefined?

    1.为什么要获取undefined? 因为undefined在javascript中不是保留字,可以被用户当做变量来赋值,这样如果我们后期需要用到undefined来检测一个变量的话,那么检测的值就不 ...

  3. ArcGIS Server For Linux 10.2.2安装

    1.#  yum install Xvfb#  yum groupinstall "X Window System"# yum install gettext 2./usr/sbi ...

  4. UISlider相关

    设置slider当前位置的图像 [slider setThumbImage:[UIImage imageNamed:@"dd.png"] forState:UIControlSta ...

  5. Ida双开定位android so文件

    Ida双开定位的意思是先用ida静态分析so文件,然后再开一个ida动态调试so文件.因为在动态调试中ida并不会对整个动态加载的so文件进行详细的分析,所以很多函数并无法识别出来.比如静态分析中有很 ...

  6. 【iOS】彩色TabBar切换动画实现

    无意间看到一个彩色TabBar切换的设计图,感觉很不错,有空就把他实现了. 环境信息 Mac OS X 10.10.4 Xcode 6.4 iOS 8.4 效果图: 效果图 源码下载地址: https ...

  7. 【代码笔记】iOS-竖排文字

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...

  8. 【代码笔记】iOS-GTMBase64

    一,工程文件. 二,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading th ...

  9. 文件与目录的rwx权限

    r(Read,读取):对文件而言,具有读取文件内容的权限:对目录来说,具有浏览目录的权限. w(Write,写入):对文件而言,具有新增.修改文件内容的权限:对目录来说,具有删除.移动目录内文件的权限 ...

  10. 【转发】网易邮箱前端技术分享之javascript编码规范

    网易邮箱前端技术分享之javascript编码规范 发布日期:2013-11-26 10:06 来源:网易邮箱前端技术中心 作者:网易邮箱 点击:533 网易邮箱是国内最早使用ajax技术的邮箱.早在 ...