python学习socket的客户端实现

服务端见点击打开链接



import socket

#ip:127.0.0.1,port 3214
HOST='' #warning:Ip addr can only be dropped in server!
PORT=3214

#AF_INET IPV4
#SOCK_DGRAM UDP
s=socket.socket()

#try to receive data from server & send data to server
#Pay attention to try statement .Client need connect to server in tcp,so you may catch exceptions,
#while in udp you just receive and send data!
try:
    s.connect((HOST,PORT))
    data=u'Hello TCP Server!'
    while data:
        #encode data from utf-8  to bytes
        s.sendall(data.encode('utf-8'))
        recv_data=s.recv(1024)
        #decode data from bytes to utf-8
        print 'Receive from Server:\n',recv_data.decode('utf-8')
        data=raw_input('Please input an info:\n')
except socket.error as err:
    print err
finally:
    s.close()
#coding:utf-8
import socket

HOST='127.0.0.1'
PORT=3214

s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

data='Hello UDP server!'

while data:
    s.sendto(data.encode('utf-8'),(HOST,PORT))
    if data == 'bye':
        break
    recv_data,addr=s.recvfrom(1024)
    print 'Receive from Server:\n',recv_data.decode('utf-8')
    data=raw_input('Please input an info:\n')
s.close()

为您推荐了相关的技术文章:

  1. MS17-010: EternalBlue’s Large Non-Paged Pool Overflow in SRV Driver - TrendLabs Security Intelligence Blog
  2. Pivoting from blind SSRF to RCE with HashiCorp Consul
  3. Tiki-Wiki, sysPass XSS Filter Bypass
  4. Eternal Champion Exploit Analysis
  5. ExPetr/Petya/NotPetya is a Wiper, Not Ransomware

原文链接: blog.csdn.net