第一范文网 - 专业文章范例文档资料分享平台

Python 的 Socket 编程教程

来源:用户分享 时间:2025/7/7 2:57:47 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

# could not resolve

print 'Hostname could not be resolved. Exiting' sys.exit()

print 'Ip address of ' + host + ' is ' + remote_ip #Connect to remote server

s.connect((remote_ip , port))

print 'Socket Connected to ' + host + ' on ip ' + remote_ip #Send some data to remote server for f in open(\ #f.close()

try : #Set the whole string s.sendall(message) except socket.error:

#Send failed

print 'Send failed' sys.exit() print f, message = f

print 'Message send successfully' #New receive data

reply = s.recv(4096) print reply s.close()

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Socketser.py代码如下:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #!/usr/bin/env python import socket import sys

from thread import *

HOST = '' # Symbolic name meaning all available interfaces PORT = 8888 # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'Socket created'

#Bind socket to local host and port try:

s.bind((HOST, PORT)) except socket.error , msg:

print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit()

print 'Socket bind complete' #Start listening on socket s.listen(10)

print 'Socket now listening'

#Function for handling connections. This will be used to create threads def clientthread(conn): #Sending message to connected client conn.send('Welcome to the server. Type something and hit enter\\n') #send only takes string

#infinite loop so that function do not terminate and thread do not end. while True:

#Receiving from client data = conn.recv(1024) reply = 'OK...' + data f=open('receive.txt', 'a') print >> f,data f.close()

if not data: break

conn.sendall(reply)

#came out of loop conn.close()

#now keep talking with the client

while 1: #wait to accept a connection - blocking call

conn, addr = s.accept() print 'Connected with ' + addr[0] + ':' + str(addr[1]) #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.

start_new_thread(clientthread ,(conn,)) s.close()

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

把客户端读取文件的方式改为:

# python socketcli.py “aaa:bbb:ccc:ddd” 代码如下:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #!/usr/bin/python

import socket # for sockets import sys # for exit try:

#create an AF_INET, STREAM socket (TCP)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, msg:

print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1] sys.exit();

print 'Socket Created' host = '10.1.22.11' port = 8888

try:

remote_ip = socket.gethostbyname( host ) except socket.gaierror: # could not resolve print 'Hostname could not be resolved. Exiting' sys.exit()

print 'Ip address of ' + host + ' is ' + remote_ip #Connect to remote server s.connect((remote_ip , port))

print 'Socket Connected to ' + host + ' on ip ' + remote_ip #Send some data to remote server def readfile(filename):

'''Print a file to the standard output.''' f = file(filename) while True:

line = f.readline() if len(line) == 0: break print line, f.close()

print \message = sys.argv[1] try : #Set the whole string s.sendall(message) except socket.error:

#Send failed print 'Send failed' sys.exit()

print 'Message send successfully' ##New receive data reply = s.recv(4096) print reply

s.close()

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

搜索更多关于: Python 的 Socket 编程教程 的文档
Python 的 Socket 编程教程.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2n5ll8r40o0ne2d1fb27_4.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top