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

python socket 简单例子

程序员文章站 2022-04-09 13:12:03
...

myserver.py:

import socket

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('', 8889))
serversocket.listen(5) # become a server socket, maximum 5 connections

print("Listening...")

while True:
connection, address = serversocket.accept()
print 'Connected with ', address
buf = connection.recv(256)
if len(buf) > 0:
print buf
break

  

myclient.py:

import socket

clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8089))
clientsocket.send('hello')