标签:python
import urllib2 import urllib response = urllib2.urlopen('http://www.baidu.com') #获取百度首页的代码,打开远程文件 urllib.urlretrieve(‘http://www.baidu.com','baidu_index.html') #下载文件并保存到本地 html = response.read() print html
import socket s = socket.socket() # host = socket.gethostname() port = 1234 s.bind((host,port)) s.listen(5) while True: c,addr = s.accept() print 'client link from:',addr c.send('Hello, client. This is server') c.close()
from SocketServer import TCPServer,StreamRequestHandler class Handler(StreamRequestHandler): def handle(self): #处理放在handle方法中 addr = self.request.getpeername() print 'get link from:',addr self.wfile.write('welcome, link succeed') server = TCPServer(('',1234),Handler) server.serve_forever() #begin to listen and handle link
#运行结果 bowen:~ bowen$ vimSocketServer_server.py bowen:~ bowen$ python SocketServer_server.py get link from: ('192.168.42.184', 49533)
import socket s = socket.socket() host = socket.gethostname() port = 1234 s.connect((host,port)) #连接服务器 print s.recv(1024) s.close()
bowen:Python bowen$ cat server.py import socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create Socket sock.bind(('localhost',8001)) #bind ip and port sock.listen(5) #start listening while True: conn,addr = sock.accept() print 'link from:',addr try: conn.settimeout(5) buff = conn.recv(1024) if buff == '1': conn.send('welcome to server') #send msg to client else: conn.send('Plead go out') except socket.timeout: print 'time out exception' conn.close() #close link bowen:Python bowen$ cat client.py import socket import time sock = socket.socket() sock.connect(('localhost',8001)) time.sleep(2) sock.send('1') print sock.recv(1024) #recv msg from server sock.close()
#运行结果 bowen:~ bowen$ vimSocketServer_server.py bowen:~ bowen$ python SocketServer_server.py get link from: ('192.168.42.184', 49533)
from SocketServer import TCPServer,ForkingMixIn,StreamRequestHandler class Server(ForkingMixIn, TCPServer): pass class Handler(StreamRequestHandler): def handle(self): addr = self.request.getpeername() print 'get link from:',addr self.wfile.write('many links by fork') if __name__ == '__main__': server = Server(('localhost',1234),Handler) server.serve_forever()
from SocketServer import TCPServer,ThreadingMixIn,StreamRequestHandler class Server(ThreadingMixIn, TCPServer): pass class Handler(StreamRequestHandler): def handle(self): addr = self.request.getpeername() print 'get link from:',addr self.wfile.write('many links in Thread way') if __name__ == '__main__': server = Server(('localhost',1234),Handler) server.serve_forever()
import socket, select s = socket.socket() s.bind(('localhost',1234)) s.listen(5) inputs = [s] #store socket req while True: rs,ws,es = select.select(inputs,[],[]) #use select() for r in rs: if r is s: conn,addr = s.accept() print 'get link form:',addr inputs.append(conn) #put socket conn into inputs else: try: data = r.recv(1024) disconnected = not data except socket.error: disconnected = True if disconnected: print r.getpeername(),' disconnected' inputs.remove(r) else: <span style="white-space:pre"> </span> print data
标签:python
原文地址:http://blog.csdn.net/dutsoft/article/details/40507773