Python服务器开发三:Socket

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、

Python服务器开发三:Socket
#!/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()
Python服务器开发三:Socket

 

文件类对象:

Python服务器开发三:Socket
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)
    
Python服务器开发三:Socket

高级接口:

Python服务器开发三:Socket
import urllib, sys

f = urllib.urlopen(sys.argv[1])
while 1:
    buf = f.read(2048)
    if not len(buf):
        break
    sys.stdout.write(buf)
Python服务器开发三:Socket

 

 

服务器端:

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、

Python服务器开发三:Socket
#!/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()
Python服务器开发三:Socket

使用UDP:

Python服务器开发三:Socket
#!/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()
Python服务器开发三:Socket
上一篇:SimpleDateFormat是线程不安全的,切忌切忌!


下一篇:用命令行编译java并生成可执行的jar包