python 14章,网络编程

时间:2014-05-02 21:59:57   收藏:0   阅读:444

一,socket 模块

套接字包括两类:服务器,客户机,

创建一个套接字后让它等待连接

服务器:

import socket
s = socket.socket()
port = 1234
host = socket.gethostname()
print host
s.bind((host,port))
s.listen(5)
while True:
    get,addr = s.accept()
    print ‘Got connection from‘,addr
    get.send(‘Thank you for connecting‘)
    get.close()

客户机:

import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.connect((host,port))
print s.recv(1024)

>>> 
Z5WRYRLDY59AL03
Got connection from (‘222.27.174.175‘, 53389)
Got connection from (‘222.27.174.175‘, 53398)
Got connection from (‘222.27.174.175‘, 53405)
Got connection from (‘222.27.174.175‘, 53407)

>>> ================================ RESTART ================================
>>> 
Thank you for connecting
>>> ================================ RESTART ================================
>>> 
Thank you for connecting
>>> ================================ RESTART ================================
>>> 
Thank you for connecting
>>> ================================ RESTART ================================
>>> 
Thank you for connecting

二,urllib,urllib2

  可以通过 网络访问文件,借用URL简单下载用urllib 如果要用http验证或用cookie 用urllib2

1.打开远程文件

from urllib import urlopen

webpage = urlopen("http://www.baidu.com")

urlopen 方法:close,read,readline,readlines

2.获取远程文件

urlretrieve(‘http://www.python.org‘,‘c:\\python.html‘)

默认放在临时位置,用urlcleanup清理

三,SocketServer

SocketServer是许多服务器框架的基础如:BaseHTTPServer,SimpleHTTPServer,CGIHTTPServer

SocketSever包含4个基本类:1针对TCP套接字流的TCPServer,2.针对UDP数据报UDPServer 3.UnixStreamServer 4.UnixDatagramServer

服务器:

from SocketServer import TCPServer,StreamRequestHandler
import socket
class Handler(StreamRequestHandler):

    def handle(self):
        addr = self.request.getpeername()
        print ‘Got connection from‘,addr
        self.wfile.write(‘Thank you for connecting!!!‘)
host = socket.gethostname()
server = TCPServer((host,1234),Handler)
server.serve_forever()
>>> 
Thank you for connecting!!!
>>>

Got connection from (‘222.27.174.175‘, 55479) 

四,多连接

3种方法: 分叉(forking),线程(threading),异步IO(asynchronous I/O)

使用SocketServer进行分叉和线程处理

from SocketServer import TCPServer,ForkingMixIn,StreamRequestHandler
import socket
class Server(ForkingMixIn,TCPServer):pass

class Handler(StreamRequestHandler):

    def handle(self):
        addr = self.request.getpeername()
        print ‘Got connection from‘,addr
        self.wfile.write(‘Thank you for connecting ...‘)
host = socket.gethostname()
server = Server((‘‘,1234),Handler)
server.serve_forever()

使用select

import socket,select
s = socket.socket()
host = socket.gethostname()
port = 3306
print host
s.bind((host,port))

s.listen(5)
inputs = [s]
while True:
    rs,ws,es = select.select(inputs,[],[])
    for r in rs:
        if r is s:
            c,addr = s.accept()
            print ‘Got connection form‘,addr
            inputs.append(c)
        else:
            try:
                data = r.recv(1024)
                disconnected = not data
                if data:
                    r.send(‘yes!I can hear you!‘)
            except socket.error:
                disconnected = True

            if disconnected:
                print r.getpeername(),‘disconnected‘
                inputs.remove(r)
            else:
                print data




python 14章,网络编程,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!