Python 官方文档:入门教程 => 点击学习
一、简单实例:实现客户端向服务端发送的信息,服务器收到信息后加上当前时间再返回给客户端服务端实现:#!/usr/bin/python import Socket, traceback, time host = '' port = 8000
一、简单实例:
实现客户端向服务端发送的信息,服务器收到信息后加上当前时间再返回给客户端
服务端实现:
#!/usr/bin/python
import Socket, traceback, time
host = ''
port = 8000
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
try:
print "Got connection from", clientsock.getpeername()
while 1:
data = clientsock.recv(4096)
if not len(data):
break
clientsock.sendall("[%s] %s" % (time.ctime(),data))
except (KeyboardInterrupt, SystemExit):
raise
except:
traceback.print_exc()
try:
clientsock.close()
except KeyboardInterrupt:
raise
except:
traceback.print_exc()
客户端实现:
#!/usr/bin/Pythonh
import socket, ssl
host = '192.168.209.128'
port = 8000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
while True:
data = raw_input('please input data:')
if not data:
break
s.sendall(data)
data = s.recv(4096)
print data
ssl_sock.close()
运行测试:
[root@localhost example]# python echoclient1.py
please input data:test python
[Sun Sep 15 14:38:47 2013] test python
please input data:test 1111111111111111111111
[Sun Sep 15 14:38:52 2013] test 1111111111111111111111
please input data:
二、socket 多任务处理
以上代码要实现的功能是实现了,但是当你用多个客户端同时执行向服务器端发送信息的时候,就发现同一时间服务端只能处理一个连接,其他的无法正常返回。这是因为服务端的单线程实现的,当收到一个客户端请求处理的时候,就会进入阻塞状态,无法同时处理多个任务。
为了能够同时为多个客户端服务,需要能够同时处理多个网络连接的方法。可通过三种方法来实现:
forking、threading和异步I/O。
通过threading实现服务端:
#!/usr/bin/python
import socket, traceback, os, sys, time
from threading import *
host = ''
port = 8000
def handlechild(clientsock):
print "New child", currentThread().getName()
print "Got connection from", clientsock.getpeername()
while 1:
data = clientsock.recv(4096)
if not len(data):
break
clientsock.sendall("[%s] %s" % (time.ctime(),data))
clientsock.close()
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
t = Thread(target = handlechild, args = [clientsock])
t.setDaemon(1)
t.start()
这样就可以让服务器同时处理多个任务了。
三、在服务端和客户端使用ssl
首先创建一个自签名证书:
openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.key
服务端:
#!/usr/bin/python
import socket, ssl, traceback, threading, sys, time
host = ''
port = 443
def Myhandlechild(clientsock):
print "New child", threading.currentThread().getName()
print "Got connection from", clientsock.getpeername()
while 1:
data = clientsock.recv(4096)
if not len(data):
break
clientsock.sendall("[%s] %s" % (time.ctime(),data))
clientsock.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(5)
while 1:
try:
clientsock, clientaddr = s.accept()
connsock = ssl.wrap_socket(clientsock, server_side=True,
certfile="cert.pem", keyfile="cert.key")
except KeyboardInterrupt:
raise
except:
traceback.print_exc()
continue
t = threading.Thread(target = Myhandlechild, args = [connsock])
t.setDaemon(1)
t.start()
客户端:
#!/usr/bin/pythonh
import socket, ssl
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ssl_sock = ssl.wrap_socket(s,ca_certs="cert.pem", cert_reqs = ssl.CERT_REQUIRED)
ssl_sock.connect(('127.0.0.1', 443))
while 1:
data = raw_input('input data:')
if not data:
break
ssl_sock.sendall(data)
data = ssl_sock.recv(4096)
print data
ssl_sock.close()
--结束END--
本文标题: python socket学习
本文链接: https://lsjlt.com/news/187260.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0