python网络socket编程

一、服务端

#!/usr/bin/python
# -*- coding: UTF-8 -*-


import socket
import sys
from thread import *
 
HOST = '127.0.0.1'   # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
 
#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
     
print 'Socket bind complete'
 
#Start listening on socket
s.listen(10)
print 'Socket now listening'
 
#Function for handling connections. This will be used to create threads
def clientthread(conn):
    #Sending message to connected client
    conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
     
    #infinite loop so that function do not terminate and thread do not end.
    while True:
         
        #Receiving from client
        data = conn.recv(1024)
        reply = '收到信息:' + data

        print reply

        reply = "my name is 徐芳波 " 
        if not data: 
            break
     
        conn.sendall(reply)
     
    #came out of loop
    conn.close()
 
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])
     
    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    start_new_thread(clientthread ,(conn,))
 
s.close()

 

二、客户端

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名:client.py


import socket               # 导入 socket 模块
import time

s = socket.socket()         # 创建 socket 对象
host = '127.0.0.1' # 获取本地主机名
port = 8888                # 设置端口好

s.connect((host, port))

while True :

    s.send("my name is 开心 " )
    data = s.recv(1024)
    print '收到信息:'+data
    time.sleep(1)

s.close()

 

三、运行服务端

root@xushi:~/ai/python# python netsharp.pigeon.py
Socket created
Socket bind complete
Socket now listening
Connected with 127.0.0.1:40114
收到信息:my name is 开心 
收到信息:my name is 开心 
收到信息:my name is 开心 
收到信息:my name is 开心 
收到信息:my name is 开心 
收到信息:my name is 开心 
收到信息:my name is 开心 
收到信息:my name is 开心 
收到信息:my name is 开心 
收到信息:my name is 开心

 

四、运行客户端

root@xushi:~/ai/python# python netsharp.pigeon.client.py 
收到信息:Welcome to the server. Type something and hit enter

收到信息:my name is 徐芳波 
收到信息:my name is 徐芳波 
收到信息:my name is 徐芳波 
收到信息:my name is 徐芳波 
收到信息:my name is 徐芳波 
收到信息:my name is 徐芳波 
收到信息:my name is 徐芳波 
收到信息:my name is 徐芳波 
收到信息:my name is 徐芳波 
收到信息:my name is 徐芳波 

 

转载于:https://www.cnblogs.com/Netsharp/p/8365065.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值