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. Codrops 实验:使用 Vibrant.js 提取图像颜色

    Codrops 分享了一个有趣的颜色提取实验.这个想法是创建图像的调色板,既有图像本身的潜移默化的影响,也有一些花哨的颜色延伸.通过使用 Vibrant.js 来提取图像中的颜色,并通过 CSS 过滤 ...

  2. Ext.store.load callback

    var paramsReceivable = {};                paramsReceivable.querytext = Ext.getCmp('hiddquerytext').g ...

  3. jQuery构造函数init参数分析(三)

    分析完了字符串情况剩下的就不多了. 5.参数selector是函数 这个就是很容易想到了,首先说一下dom加载.我们通常在head里面写脚本的时候需要等待文档加载在进行处理,js是这么写的 windo ...

  4. ABAP 动态生成内表的几种方法

    最近要写个程序,既有更新的,也有删除的,需要涉及到很多系统表,如果一个表一个表进行更新或者删除太慢了,于是就想通过创建动态内表来实现这些功能,在网上找了一些资料,经过多次尝试,终于测试成功了.网上讲述 ...

  5. 导入CSV格式的数据

    导入CSV格式的数据 (参见http://dev.mysql.com/doc/refman/5.6/en/load-data.html) 1.数据库表(st_pptn_r) CREATE TABLE ...

  6. GP服务将矢量数据加入到栅格数据中的方法

    1.如何将矢量数据加入栅格数据中 1.  Conversion Tools -->To Raster-->Polygon to Raster 2. Spatial Analyst Tool ...

  7. 学习笔记:腾讯云——服务器mysql操作

    1.进入数据库 (注意:在linux系统下要进入mysql所在的文件夹下才能打开数据库) 操作1:进入到指定目录下 命令行:cd /opt/lampp/bin 操作2:进入到数据库 命令行:./mys ...

  8. Ubuntu 16.04 LTS 安装配置 Nginx 1.10.0 Php7.0-FPM

    1. 安装Nginx,Php-7.0 ~$ sudo add-apt-repository ppa:nginx/stable ~$ sudo apt-get update ~$ sudo apt-ge ...

  9. android加固系列—5.加固前先学会破解,hook(钩子)jni层系统api

    [版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5138585.html] crackme项目jni的关键代码(项目地址见文章底部),获取当前程序 ...

  10. iOS 边框显示不完全

    设置边框为1以上就可以了,如果不觉得宽的话!