A bit of socket code from python
来源:互联网 发布:华中数控铣床编程实例 编辑:程序博客网 时间:2024/04/25 11:34
Sockets are used nearly everywhere.This is a bit of socket code form python.This program is designed to test FRS4 socket communicate test as below.
import socket
print "-----------------------------------------------------------"
print "Welcom to use Python Socket Tools./nThis program is designed to test FRS4 socket communicate test/n----Powered by Lucker."
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create a socket
try:
s.connect((socket.gethostname(), 8888)) #Connect to target server
except:
print "Connect to '%s' failed!"%(socket.gethostname())
else:
phoneno = raw_input("Please input your PhoneNo.:(Push Enter to use default)") #Get local phoneno
if (len(phoneno)) == 0:
phoneno= '123456'
phoneno="phone:"+phoneno #Add a prefix of phoneno
s.send(phoneno) #Send data string to server
print '>> %s'%phoneno
data = s.recv(4096) #Get response data from server
print '<< %s'%data
process_string='*571253*61235468*1*2617*5#'
s.send(process_string)
print '>> %s'%process_string
print '<< %s'%s.recv(4096)
s.send('6506')
print '>> %s'%'6506'
print '<< %s'%s.recv(4096)
s.close() #Disconnect the socket connection
print 'End!' #End the program
Meybe,we need a special class for our communication in the socket program.We call it "MySocket" as below
class mysocket:
'''demonstration class only coded for clarity, not efficiency '''
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def connect(self, host, port):
self.sock.connect((host, port))
def mysend(self, msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError,"socket connection broken"
totalsent = totalsent + sent
def myreceive(self):
msg = ''
while len(msg) < MSGLEN:
chunk = self.sock.recv(MSGLEN-len(msg))
if chunk == '':
raise RuntimeError,"socket connection broken"
msg = msg + chunk
return msg
- A bit of socket code from python
- a bit of java (from forum)
- compile a string of python code
- a bit of git
- A example code of multithread communication in Python
- a clump of code
- Write efficient code for extracting unique elements from a sorted list of array. e.g.
- Accessing 64 bit registry from a 32 bit process
- learn from tutorials of source code
- Code Like a Pythonista: Idiomatic Python - Python idioms, 3 of n
- Print the bit pattern of a float
- A bit of fun: fun with bits
- A little bit of sad and idle
- Feeling a bit of tired today!
- A bit of reflection on C# reflection
- A bit of fun: fun with bits
- Launching a midlet from symbian C++ code
- call a JavaScript function from C++ code
- http://blog.csdn.net/dairyman000/archive/2003/07/16/14970.aspx
- C#代码与javaScript函数的相互调用
- python win32com.client
- 系统服务的一点说明
- Java核心API需要掌握的程度 - 转自CSDN
- A bit of socket code from python
- linux安装网卡e1000e
- 尽可能的达到你们的要求
- 使用mysql截取、连接字符串
- zlib123开源压缩库的一些使用。
- 开始的地方
- pku 1696 Space Ant(转角,凸包扩展)
- 我的第一篇博客
- 网路安全并不只是边界保护