python之socket模块
2014-04-29 17:04
483 查看
UDP
client
server
TCP
client
server
测试连接MySQL端口,完成tcp三次握手
http://www.open-open.com/lib/view/open1342570701932.html
/article/4906538.html
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三次握手
http://www.open-open.com/lib/view/open1342570701932.html
/article/4906538.html
相关文章推荐
- Python自动化之socketserver模块
- python常用模块与socket编程
- python:使用socket模块,进行服务器与客户端简单交互
- python-socket模块介绍
- python小白-day8 socketserver模块
- Python网络通信之黏包问题(五)基于SocketServer模块和socket模块
- python的Socketserver模块使用,异常处理
- Python网络编程(3)——SocketServer模块与简单并发服务器
- Python网络通信之socket模块(一)关于套接字
- Python Socket模块中的IP转换函数
- python的socket模块
- Python笔记:socket模块的getaddrinfo详解
- python socket模块
- python socket编程(二)--SocketServer模块
- Python的Asyncore异步Socket模块及实现端口转发的例子
- python网络编程socket模块实现ftp上传下载
- 树莓派 - 2 Python - b 网络与进程/线程模块:socket, threading, subprocess
- python模块介绍- SocketServer 网络服务框架
- python 网络编程之socketserver模块