免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2401 | 回复: 0
打印 上一主题 下一主题

一个简单的TCP客户端与服务器 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-23 23:45 |只看该作者 |倒序浏览
服务器端功能:监听本地50007端口,最多处理5个客户请求,接收来自客户端的消息,并反射回去。
#!/usr/bin/env python
#########################################################
# Server side: open a socket on a port, listen for
# a message from a client, and send an echo reply;
# this is a simple one-shot listen/reply per client,
# but it goes into an infinite loop to listen for
# more clients as long as this server script runs;
#########################################################
from socket import * # get socket constructor and constants
myHost = '' # server machine, '' means local host
myPort = 50007 # listen on a non-reserved port number
sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP socket object
sockobj.bind((myHost, myPort)) # bind it to server port number
sockobj.listen(5) # listen, allow 5 pending connects
while 1: # listen until process killed
    connection, address = sockobj.accept() # wait for next client connect
    print 'Server connected by', address # connection is a new socket
    while 1:
        data = connection.recv(1024) # read next line on client socket
        if not data: break # send a reply line to the client
        connection.send('Echo=>' + data) # until eof when socket closed
    connection.close()
客户端功能:连接至服务器,向服务器发送消息,并显示服务器返回的消息。
#!/usr/bin/env python
#############################################################
# Client side: use sockets to send data to the server, and
# print server's reply to each message line; 'localhost'
# means that the server is running on the same machine as
# the client, which lets us test client and server on one
# machine; to test over the Internet, run a server on a remote
# machine, and set serverHost or argv[1] to machine's domain
# name or IP addr; Python sockets are a portable BSD socket
# interface, with object methods for standard socket calls;
#############################################################
import sys
from socket import * # portable socket interface plus constants
serverHost = 'localhost' # server name, or: 'starship.python.net'
serverPort = 50007 # non-reserved port used by the server
message = ['Hello network world'] # default text to send to server
if len(sys.argv) > 1:
    serverHost = sys.argv[1] # or server from cmd line arg 1
    if len(sys.argv) > 2: # or text from cmd line args 2..n
        message = sys.argv[2:] # one message for each arg listed
sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP/IP socket object
sockobj.connect((serverHost, serverPort)) # connect to server machine and port
for line in message:
    sockobj.send(line) # send line to server over socket
    data = sockobj.recv(1024) # receive line from server: up to 1k
    print 'Client received:', `data`
sockobj.close() # close socket to send eof to server
代码来自>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/19742/showart_366441.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP