欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python socket编程代码实例

程序员文章站 2022-06-24 22:54:40
client端学会数据的发送哦 # -*- coding: utf-8 -*- import socket import re import time s = socket.so...

client端学会数据的发送哦

# -*- coding: utf-8 -*-

import socket  
import re
import time
s = socket.socket()
host = "xx.xxx.xxx.xxx" # 获取本地主机名
port = xxxx
s.connect((host, port))
while 1:
 data = s.recv(1024)
 print data
 l = []
 for k in range(len(data)):
  if data[k]=='+':
l.append('+')
  elif data[k]=='-':
l.append('-')
 q=re.findall(r'\d+',data)
 if len(q)==10:
  sum = int(q[0])
  for i in range(1,10):
print sum
if l[i-1]=='+':
 sum= sum + int(q[i])
elif l[i-1]=='-':
 sum  =  sum-int(q[i])
  print 'sum='+str(sum)
  emm = str(sum)
  s.send(emm+'\r\n')
  # 果然好多坑
  print s.recv(1024)
s.close()