python server and client

python server and client

Overview:

They server is run by python3, while client is python2.7.

Server:

“python

from socket import *

ServerUrl = ":9867"

def run():
   global IP
   global PORT
   try:
       Colon = ServerUrl.find(':')
       IP = ServerUrl[0:Colon]
       Port = int(ServerUrl[Colon+1:])

   print ('Server ServerUrl %s' % ServerUrl)
   print ('Server IP:  %s' % IP)
   print ('Server Port:  %d' % Port)
   #sockobj = socket(AF_INET, SOCK_STREAM)
   sockobj = socket(AF_INET, SOCK_STREAM)
   sockobj.setsockopt(SOL_SOCKET,SO_REUSEADDR, 1)


   sockobj.bind((IP, int(Port)))

   sockobj.listen(5)
   print ('====Server here ===')

   while True:
       connection, address = sockobj.accept()

       print ('Server connected by client:%s' % str(address))
       while True:
           data = connection.recv(1024)

           if not data:
               break

           ret='200 OK'
           connection.send(ret.encode('utf-8'))
           print ('Receive MSG:%s' % data.strip())
           print ('Send ret:%s\r\n' % ret)

   connection.close( )

   except Exception as e:
       print ("The issue:%s" % e)

if __name__ == '__main__':
    run()

Client

“python

import socket

HOST = '10.86.79.59'
PORT = 9867

request = 'can you hear me?'

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

s.sendall(request)

reply   = s.recv(1024)
print 'reply is: ',reply

s.close()

Test

Server

Client

阅读更多
文章标签: python socket
个人分类: Python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭