码迷,mamicode.com
首页 > 编程语言 > 详细

Python服务器开发三:Socket

时间:2014-07-09 23:37:56      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:blog   http   使用   文件   os   cti   

socket是操作系统中I/O的延续,它可以使进程和机器之间的通信成为可能。socket可以看成一个标准的文件描述符。不同的是文件需要用open()函数打开,而socket用socket()

函数建立.recv()、send()函数和read()、write()函数极为相似。

TCP一般通过accept()来为每个连接的客户端建立一个新的scoket。UDP一般只是使用一个单一的socket,完全依靠recvfrom()返回的值来判断该往哪里发送响应。

 

客户端:

1、建立一个socket:

//SOCK_STREAM表示TCP,SOCK_DGRAM表示UDP,AF_INET表示IPv4
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

2、连接socket

连接socket需要一个tuple参数,来提供IP和端口号:

s.connect((“www.example.com”, 80))

3、

bubuko.com,布布扣
#!/usr/bin/env python

import socket

print "Creating socket...",
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "done."

print "Looking up port number...",
port = socket.getservbyname(‘http‘, ‘tcp‘)
print "done."

print "Connecting to remote host on port %d..." % port,
s.connect(("www.google.com", port))
print "done."

#获取本身的IP和端口号
print "Connected from", s.getsockname()
#获取远程的IP和端口号
print "Connected to", s.getpeername()
bubuko.com,布布扣

 

文件类对象:

bubuko.com,布布扣
import socket, sys

port = 70
host = sys.argv[1]
filename = sys.argv[2]

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
fd = s.makefile(‘rw‘, 0)

fd.write(filename + "\r\n")

for line in fd.readlines():
    sys.stdout.write(line)
    
bubuko.com,布布扣

高级接口:

bubuko.com,布布扣
import urllib, sys

f = urllib.urlopen(sys.argv[1])
while 1:
    buf = f.read(2048)
    if not len(buf):
        break
    sys.stdout.write(buf)
bubuko.com,布布扣

 

 

服务器端:

1、建立一个连接:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

2、设置socket选项:

//socket 端口可立即复用
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

3、绑定socket:

s.bind((host, port))

4、监听:

s.listen(1)

5、接受连接:

while 1:
    clientsock, clientaddr = s.accept()
    print "Got connection from", clientsock.getpeername()
    clientsock.close()

6、

bubuko.com,布布扣
#!/usr/bin/env python

import socket, traceback

host = ‘‘                               # Bind to all interfaces
port = 51423

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
#这里设定每次最多只有一个等候处理的请求,真正的服务器会设置一个很高的数字。 s.listen(1) while 1: try: clientsock, clientaddr = s.accept() except KeyboardInterrupt: raise except: traceback.print_exc() continue # Process the connection try: print "Got connection from", clientsock.getpeername() # Process the request here except (KeyboardInterrupt, SystemExit): raise except: traceback.print_exc() # Close the connection try: clientsock.close() except KeyboardInterrupt: raise except: traceback.print_exc()
bubuko.com,布布扣

使用UDP:

bubuko.com,布布扣
#!/usr/bin/env python

import socket, traceback

host = ‘‘                               # Bind to all interfaces
port = 51423

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))

while 1:
    try:
        message, address = s.recvfrom(8192)
        print "Got data from", address
        # Echo it back
        s.sendto(message, address)
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        traceback.print_exc()
bubuko.com,布布扣

Python服务器开发三:Socket,布布扣,bubuko.com

Python服务器开发三:Socket

标签:blog   http   使用   文件   os   cti   

原文地址:http://www.cnblogs.com/L-H-R-X-hehe/p/3812630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!