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. FancyBox - 经典的 jQuery Lightbox 插件

    FancyBox 是一款非常优秀的弹窗插件,能够为图片.HTML 内容和其它任务的多媒体内容提供优雅的弹出缩放效果.作为是最流行的 Lightbox 插件之一,可以通过 fitToView 实现自适应 ...

  2. jQuery 浮动标签插件,帮助你提升表单用户体验

    浮动标签模式(Float Label Pattern)是最新流行的一种表单输入域的内容提示方式,当用户在输入框输入内容的时候,原先占位符的内容向上移动,显示在输入的内容的上面.这里推荐的这款 jQue ...

  3. 站长必备:10个好用的 WordPress 备份插件

    网站备份对于站长来说极其重要的.任何的事情都可能发生,这可能会导致你失去所有的辛勤工作:您的网站可能被黑客攻破,你可以安装一个了插件导致冲突,你的服务器可能被攻击,你可能在编辑文件时犯了一个错误等等, ...

  4. 免费素材:包含 250+ 组件的 DO UI Kit

    DO UI kit 现在可以免费用于 Photoshop 和 Sketch 了.它有超过130个屏幕,10个完整的主题以及250+的组件混合以创造惊人的应用.他们都是再混合和视网膜.最重要的是他们看起 ...

  5. javscript闭包的准备工作 -- 作用域与作用域链

    作用域是JavaScript最重要的概念之一,想要学好JavaScript就需要理解JavaScript作用域和作用域链的工作原理.今天这篇文章对JavaScript作用域和作用域链作简单的介绍,希望 ...

  6. ae_feature的插入、复制和删除

    1.插入 /// <summary> ///向featureclass中批量插入features ,批量插入features,用buffer的方法,要比循环一个个Store的方法快 /// ...

  7. An interesting experiment on China’s censorship

    This paper presented a very interesting topic. Censorship in China has always drawn people's attenti ...

  8. 详细对比IB开发与纯手码开发的优劣。

    1.IB是什么? Interface Builder 是一种通过图形化界面搭建UI的方式,并把窗口.菜单栏以及窗口上的各种控件的对象都“冻结”在了一个 NIB文档里:程序运行时,这些对象将会“苏醒”. ...

  9. iOS开发之邓白氏编码申请流程

    要申请企业证书,必须先申请邓白氏编码,在苹果网站有一个免费申请邓白氏编码的链接:https://developer.apple.com/program/enroll/dunsLookupForm.ac ...

  10. iOS 从Xcode看应用支持横竖屏

    要看一个应用是否支持横竖屏,要看Xcode里面的info.plist文件设置才清楚,每一个新建工程都会包含三个支持方式,即Supported interface orientations里面的就是 P ...