UDP

client

#!/usr/bin/env python2.7
#-*-coding:utf-8 -*- import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto("hello",("localhost",8001)) data,addr = s.recvfrom(1024)
print "receive data:%s from %s" % (data,str(addr))

server

#!/usr/bin/env python2.7
#-*-coding:utf-8 -*- import socket port=8001
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(("",port)) while True:
data,client = s.recvfrom(1024)
print "receive a connection from %s" % str(client) s.sendto("echo:"+data,client)

TCP

client

#!/usr/bin/env python2.7
#-*-coding:utf-8 -*- import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) host="localhost"
port=5531 s.connect((host,port))
msg=raw_input("Msg:") s.send(msg) data=s.recv(1024) print "Reply from server----%s" % data

server

#!/usr/bin/env python2.7
#-*-coding:utf-8-*- import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) host = "localhost"
port = 1235 s.bind((host,port))
s.listen(3) while True:
client,ipaddr = s.accept()
print "Got a connect from %s" % str(ipaddr)
data = client.recv(1024)
print "receive data:%s" % data client.send("echo:"+data)
client.close()

测试连接MySQL端口,完成tcp三次握手

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnMAAACUCAIAAAB3DDhKAAAWL0lEQVR4nO2dzbGsOBJGy5yxAYOIaDfGAjyYXrQNPDPaAFazeqtZtQHMgirQT2ZKAkEJ7jkxET2vhKTUz9WHUkJ6/fnnn3/88ccrj26Y5oWxl8L7UQ26KTVK1A3TPA1dJYuOYpUo1b7H2FI/Lw8793ZaAQAeTZGyAgAAQIIGlXWZ2zxq6luZ5/kGAAAeBMp6Q1BWAICGaVBZ26MbpqaU7LiytlYiAIAHgbJm0JoOoawAAA0TKms/Wjsoj4Sm8LeO+gm9N3b24xrk/N9NaZwkQtVYnhcT32Qm3hvrRdPi64TRw4iWVUJ0zyqnhO/nvDJ71Rk/u7dEAACQQpizeqIVcSQ0k36UlPUtEP04z9M0fXR07F+bWnwidcP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YnFuBs5RVJlHP0uPBnCYaPSUntDzTzzUw/eLl1ECGsq7PJ/9a/Ny/NWcV3xSrKas3pct6cchBdp1qPda23u7McZ+chmHcXDzjsA0xed5gY8660zcOEbayKuPD+2XKHjiK43Zd2KhXrLOGWQUvm9Gb6DSVzFkVXV0TDl8SP1Vg9+lQ+yWbstpITtkYEbphnoblx3Gcx76PlDUh80rBAmEIjM9RVjVfq567YYzT0J0Bur9Ce3fS2tfPQX5/XH/+OAcLlFWZDZWV1883bbWB3YK+HUrPTQ/zukD5vtOwIOGyQt9n/M0EzSvNUbUS2a2QaiM/46iy7NHMbDmr10EpgrJ6ywNivRrqWCuu5JeIKZ2naf3KS17+6/sEGUOhKBimiVGxvG5t9Gkn4tjrTqBdM1mrMrohaoMyZQ2XkN9ljNTJnAG8SpQ1KpKTbfS7rsxCp3QeMMcqK76qBkILlyir/G6aUV6nuIvT8QpvsJftZxkxMk/sOWqKscH2iGR2ArVEm03im4BYIjvDpDl+wwrNbP8Bmy2n9joohRP5C5F7mzgCGRPWL+GPTSHyWJU71PxYDGVNc9rgVceTl5lKab9qn2SJ8JSCCcqawPcNhbPD7R1P3Gv1CEmqtoz7UPxBuLiejC500KYK4z76oUHNgAnKmiB0zvDHBO2y9dY6/RT90KBmwKQVZf3Pv6z/AQAA3AWUFQAAoCYoKwAAQEX+Dw44WW/Lq54FAAAAAElFTkSuQmCC" alt="" />

http://www.open-open.com/lib/view/open1342570701932.html

http://www.cnblogs.com/GarfieldTom/archive/2012/12/16/2820143.html

python之socket模块的更多相关文章

  1. 老李分享:使用 Python 的 Socket 模块开发 UDP 扫描工具

    老李分享:使用 Python 的 Socket 模块开发 UDP 扫描工具 poptest是业内唯一的测试开发工程师培训机构,测试开发工程师主要是为测试服务开发测试工具,在工作中要求你做网络级别的安全 ...

  2. python中socket模块详解

    socket模块简介 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.socket通常被叫做"套接字",用于描述IP地址和端口,是一个通信 ...

  3. Python基于socket模块实现UDP通信功能示例

    Python基于socket模块实现UDP通信功能示例 本文实例讲述了Python基于socket模块实现UDP通信功能.分享给大家供大家参考,具体如下: 一 代码 1.接收端     import ...

  4. 【Python】socket模块应用

    [Socket] 本文记录了一些socket模块的简单应用,对于具体原理还没来得及深究. ■ 利用socket模块进行端口连接验证和扫描 在linux中常用nc命令来进行远端端口是否开放的验证.但是这 ...

  5. python基础===socket模块的讲解(转)

    一.网络知识的一些介绍 socket 是网络连接端点.例如当你的Web浏览器请求www.jb51.net上的主页时,你的Web浏览器创建一个socket并命令它去连接 www.jb51.net的Web ...

  6. python之socket模块详解--小白博客

    主要是创建一个服务端,在创建服务端的时候,主要步骤如下:创建socket对象socket——>绑定IP地址和端口bind——>监听listen——>得到请求accept——>接 ...

  7. 使用 Python 的 Socket 模块构建一个 UDP 扫描工具

    译文:oschina 英文:bt3gl 当涉及到对一些目标网络的侦察时,出发点无疑是首先发现宿主主机.这个任务还可能包含嗅探和解析网络中数据包的能力. 几周前,我曾经谈到了如何使用Wireshark来 ...

  8. Python的socket模块与交互式指令

    socket简介 在编程的过程中,我们需要使用网络编程,这时我们不得不和网络通信的底层基础打交道了.我们必须让自己传输的数据符合网络通信的基本协议,即TCP/IP协议,但是网络通信协议本身很复杂.我们 ...

  9. python的socket模块

    sk.bind(address) s.bind(address) 将套接字绑定到地址.address地址的格式取决于地址族.在AF_INET下,以元组(host,port)的形式表示地址. sk.li ...

随机推荐

  1. GBK和UTF8有什么区别

    GBK编码:是指中国的中文字符,其它它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符仅能存储简体中文字符. UTF-8编码:它是一种全国家通过的一种编码,如果你的网站涉及到多 ...

  2. SQLite中的时间日期函数

    SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间 date()...........................产生日期 t ...

  3. 图片载入状态判断及实现百分比效果loading

    前言 一些大的外部资源会导致页面加载速度慢,这时候一般会加上loading效果:这里实现的是根据图片加载进度的百分比loading效果 如何判断图片加载的状态 1.onload  onerror 推荐 ...

  4. EsRejectedExecutionException排错与线程池类型

    1.EsRejectedExecutionException异常示例 java.util.concurrent.ExecutionException: RemoteTransportException ...

  5. C# 动态加载卸载 DLL

    我最近做的软件,需要检测dll或exe是否混淆,需要反射获得类名,这时发现,C#可以加载DLL,但不能卸载DLL.于是在网上找到一个方法,可以动态加载DLL,不使用时可以卸载. 我在写一个WPF 程序 ...

  6. 【ASP.NET MVC 学习笔记】- 08 URL Routing

    本文参考:http://www.cnblogs.com/willick/p/3343105.html 1.URL Routing告诉MVC如何正确的定位Controller和Action. 2.URL ...

  7. Android 分包 MultiDex 策略总结

    1.分包背景 我们在Android开发中,会不断的在App代码里面增加新功能,引入新的类库,如果不加控制的话,那么会碰到编辑器IDE爆出一下错误: Error:Execution failed for ...

  8. H5前端上传文件的几个解决方案

    目前,几个项目中用到了不同的方法,总结一下分享出来. 第一种,通过FormData来实现. 首先,添加input控件file. <input type="file" name ...

  9. component及刚体rigidbody用法

    关于getcomponent函数,rigidbody(2d)的嵌套关系及用法 1.getcomponent函数 在unity中脚本可以看成是可定义的组件,我们经常要访问同一对象或不同对象中的脚本,可以 ...

  10. 快速部署PostgreSQL

    PostgreSQL通常也简称Postgres,是一个关系型数据库管理系统,适用于各种Linux操作系统.Windows.Solaris.BSD和Mac OS X.本文基于CentOS7,使用yum源 ...