码迷,mamicode.com
首页 > Web开发 > 详细

httpd服务和apache

时间:2018-01-25 00:18:50      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:绑定   python   bind   mes   buffer   服务   nec   add   绑定ip   

套接字相关的系统调用:
socket(): 创建一个套接字
bind():绑定IP和端口
listen():监听
accept():接收请求
connect():请求连接建立
write():发送
read():接收
close():关闭连接

socket通信示例:

服务器端tcpserver.py

#/usr/bin/python
import socket
HOST=‘127.0.0.1‘
PORT=9527
BUFFER=4096
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind((HOST,PORT))
sock.listen(3)
print(‘tcpServer listen at: %s:%s\n\r‘ %(HOST,PORT))
while True:
client_sock,client_addr=sock.accept()
print(‘%s:%s connect‘ %client_addr)
while True:
recv=client_sock.recv(BUFFER)
if not recv:
client_sock.close()
break
print(‘[Client %s:%s said]:%s‘ %(client_addr[0],client_addr[1],recv))
client_sock.send(‘tcpServer has received your message‘)
sock.close()

客户端tcpclient

#/usr/bin/python
import socket
HOST=‘127.0.0.1‘
PORT=9527
BUFFER=4096
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((HOST,PORT))
sock.send(‘hello, tcpServer!‘)
recv=sock.recv(BUFFER)
print(‘[tcpServer said]: %s‘ % recv)
sock.close()

执行下面命令测试
python tcpserver
phthon tcpclient

httpd服务和apache

标签:绑定   python   bind   mes   buffer   服务   nec   add   绑定ip   

原文地址:http://blog.51cto.com/11034229/2064818

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!