Python:2个套接字,从A发送到B,从B发送到A.
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python:2个套接字,从A发送到B,从B发送到A.,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3119字,纯文字阅读大概需要5分钟。
内容图文
![Python:2个套接字,从A发送到B,从B发送到A.](/upload/InfoBanner/zyjiaocheng/776/69fdcb10dea44e27bdfd776cc4923727.jpg)
我已经为此工作了2天,但仍然没有让它正常工作.
我想编写一个使用2个插槽且中间有介质的应用程序
此介质是此脚本,应从socketA读取并写入SocketB并从SocketB读取并写入SocketA.
然而,似乎我无法指出它.
我的脚本在运行时接受连接,但它不允许我在telnet屏幕上输入内容.
我在套接字之间使用2个共享列表来传递数据.
#!/usr/bin/env python
import sys
import arduinoReadThread
import arduinoWriteThread
import socket
import thread
bolt = 0
socketArray=list()
HOST =""
HOST2=""
PORT1 =50115
PORT2 =50125
s1=socket.socket(socket.AF_INET, socket.SOCK_STREAM ) #create an INET, STREAMing socket
s1.bind((HOST,PORT1)) #bind to that port
s1.listen(2) #listen for user input and accept 1 connection at a time.
socketArray.append(s1)
s2=socket.socket(socket.AF_INET, socket.SOCK_STREAM ) #create an INET, STREAMing socket
s2.bind((HOST2,PORT2)) #bind to that port
s2.listen(2) #listen for user input and accept 1 connection at a time.
socketArray.append(s2)
print "sockets set up"
s1ToWriteList = list()
s2ToWriteList = list()
def socketFunctionWrite1():
while(bolt == 0):
client, address = s1.accept()
print "Writing connections"
if len(s1ToWriteList) > 0:
client.send(s1ToWriteList.pop(0))
def socketFunctionRead1():
while(bolt == 0):
client2, address = s2.accept()
f = client2.recv(1024)
print "reading connection"
s1ToWriteList.append(f)
print len(s1ToWriteList)
def socketFunctionWrite2():
while(bolt == 0):
client2, address = s2.accept()
print "Writing connections"
if len(s2ToWriteList) > 0:
client2.send(s2ToWriteList.pop(0))
def socketFunctionRead2():
while(bolt == 0):
client, address = s1.accept()
f = client.recv(1024)
print "reading connection"
s2ToWriteList.append(f)
print len(s2ToWriteList)
def shutDown():
test = raw_input("Quit ?")
if(test =="y"):
bolt = 1
else:
shutDown()
def spreadSockets():
thread.start_new_thread(socketFunctionRead1,())
print "launch 1"
thread.start_new_thread(socketFunctionRead2,())
print "launch 2"
thread.start_new_thread(socketFunctionWrite1,())
print "launch 3"
thread.start_new_thread(socketFunctionWrite2,())
print "launch 4"
spreadSockets()
while(True):
pass
解决方法:
使用您的确切代码,它对我有用.我认为你可能做错的是telnet到错误的IP.不要使用’localhost’或127.0.0.1,您需要使用您的盒子的实际(内部)IP.
如果在linux上,您可以在Windows上查看ifconfig -a或ipconfig / all.
完全运行代码,无需修改(除了删除顶部的2个未知导入):
推出脚本:
[ 15:01 jon@hozbox.com ~/SO/python ]$./sock.py
sockets set up
launch 1
launch 2
launch 3
launch 4
Writing connections
Writing connections
^CTraceback (most recent call last):
File "./sock.py", line 93, in <module>
time.sleep(1)
KeyboardInterrupt
然后telnet’d:
[ 15:01 jon@hozbox.com ~ ]$telnet 10.10.1.11 50115
Trying 10.10.1.11...
Connected to 10.10.1.11.
Escape character is '^]'.
Hello, World!
Hello 2
^]
telnet> quit
Connection closed.
[ 15:02 jon@hozbox.com ~ ]$telnet 10.10.1.11 50125
Trying 10.10.1.11...
Connected to 10.10.1.11.
Escape character is '^]'.
Hello 50125!
Hi!
^]
telnet> quit
Connection closed.
[ 15:02 jon@hozbox.com ~ ]$
我的内部接口配置(inet addr:10.10.1.11):
[ 15:07 jon@hozbox.com ~/SO/python ]$ifconfig eth0
eth0 Link encap:Ethernet HWaddr **:**:**:**:**:**
inet addr:10.10.1.11 Bcast:10.10.1.255 Mask:255.255.255.0
...
内容总结
以上是互联网集市为您收集整理的Python:2个套接字,从A发送到B,从B发送到A.全部内容,希望文章能够帮你解决Python:2个套接字,从A发送到B,从B发送到A.所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。