python3 socket實現簡單鏈接

#!/usr/bin/env python
# -- encoding: utf-8 --
'''
@Author : {liush}
@License : (C) Copyright 2018-2037, {liush}
@Contact : {lumia98@vip.qq.com}
@Software: PyCharm
@File : Servers.py
@Time : 2018/9/2 11:28
@Desc : socket服務端
'''
import socketpython

ip = '127.0.0.1'
port = 8000服務器

server_socket = socket.socket()
server_socket.bind((ip, port))
server_socket.listen()socket

while 1:
client_socket, client_addr = server_socket.accept()
print("接收到客戶端{}的請求,端口{}".format(client_addr[0], client_addr[1]))
data = client_socket.recv(1024)
if data:
print("----->客服端發來的數據{}".format(data.decode('utf-8')))
file = open('data.txt',mode='a+', encoding='utf-8')
file.write(data.decode('utf-8'))
file.close()
else:
breakide

print("發送完成")
server_socket.close()code

客戶端orm

import socketserver

ip = '127.0.0.1'
port = 8000對象

client_socket = socket.socket() #建立socket對象
client_socket.connect((ip,port)) #建立鏈接
print('正在鏈接{}服務器,鏈接端口{}'.format(ip,port))ip

data = input(">>>>>>>>>>")
client_socket.send(data.encode())utf-8

print("鏈接完成")client_socket.close()

相關文章
相關標籤/搜索