浏览 17606 次
锁定老帖子 主题:Python 客户端Socket编程
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-04-22
第2章 网络客户端 import socket print "Creating socket...." # socket.socket(协议,通讯方式) 创建socket对象 # socket.AF_INET使用PIV4协议; # socket.SOCK_STREAM TCP通讯方式 # socket.SOCK_DGTAM UDP通讯方式 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'Done' print 'Connecting to remote host...' # 连接服务器: IP,port s.connect(("10.10.151.70", 8080)) print 'done.'
import socket print 'Create socket...' s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'done' print 'Looking up port number...' # 通过socket的getservbyname()函数查询端口号 port = socket.getservbyname('http', 'tcp') print 'done.' print 'Connecting to remote host on port %d...' % port s.connect(('www.google.com', port)) print 'done.'
import socket print 'Creating socket...' s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'done.' s.connect(('localhost', 8080)) print 'done.' # 显示本机的IP地址和端口号 print 'Connected from', s.getsockname() # 显示远端服务器的IP地址和端口号 print 'Connected to ', s.getpeername()
import socket, sys host = sys.argv[1] #主机IP textport = sys.argv[2] #端口号 filename = sys.argv[3] #服务器文件名称 #建立socket对象 try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, e: print 'Strange error creating socket:%s' % e sys.exit(1) #解析端口号 try: port = int(textport) except ValueError: try: port = socket.getservbyname(textport, 'tcp') except socket.error, e: print 'Couldn not find your port:%s' % e sys.exit(1) #创建连接 try: s.connect((host, port)) except socket.gaierror, e: print 'Address-related error connecting to server :%s' % e sys.exit(1) except socket.error, e: print 'Connection error:%s' % e sys.exit(1) #发送文件 try: s.sendall("GET %s HTTP/1.0\r\n\r\n" % filename) except socket.error, e: print 'Error sending data:%s' % e sys.exit(1) #清除缓存内容 try: s.shutdown(1) except socket.error, e: print 'Error sending data:%s' % e sys.exit(1) while True: try: buf = s.recv(2048) #接收数据 except socket.error, e: print 'Error receiving data:%s' % e sys.exit(1) if not len(buf): break sys.stdout.write(buf) mport socket, sys, time host = sys.argv[1] textport = sys.argv[2] filename = sys.argv[3] #创建socket对象 try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, e: print 'Strange error creating socket:%s' % e sys.exit(1) #创建端口号 try: port = int(textport) except ValueError: try: port = socket.getservbyname(textport, 'tcp') except socket.error, e: print 'Couldn not find your port:%s' % e sys.exit(1) #连接socket try: s.connect((host, port)) except socket.gaierror, e: print 'Address-related error connecting to server :%s' % e sys.exit(1) except socket.error, e: print 'Connection error:%s' % e sys.exit(1) fd = s.makefile('rw', 0) #使用文件类对象 print 'sleeping...' time.sleep(10) #线程休息10S print 'Continuing...' try: s.sendall("GET %s HTTP/1.0\r\n\r\n" % filename) except socket.error, e: print 'Error sending data:%s' % e sys.exit(1) try: fd.flush() except socket.error, e: print 'Error sending data: %s' % e sys.exit(1) try: s.shutdown(1) s.close() except socket.error, e: print 'Error sending data:%s' % e while True: try: buf = fd.read(2048) except socket.error, e: print 'Error receiving data:%s' % e sys.exit(1) if not len(buf): break sys.stdout.write(buf)
import socket, sys host = sys.argv[1] textport = sys.argv[2] # 使用SOCK_DGRAM 报文 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: port = int(textport) except ValueError: # 使用UDP协议 port = socket.getservbyname(textport, 'udp') s.connect((host, port)) print 'Enter data to transmit:' data = sys.stdin.readline().strip() s.sendall(data) print 'Looking for replies; press Ctrl-C or Ctrl-Break to stop.' while True: buf = s.recv(2048) if not len(buf): break sys.stdout.write(buf)
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |