Coverage for pyilper/pilsocket.py: 81%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/python3
2# -*- coding: utf-8 -*-
3# pyILPER 1.2.1 for Linux
4#
5# An emulator for virtual HP-IL devices for the PIL-Box
6# derived from ILPER 1.4.5 for Windows
7# Copyright (c) 2008-2013 Jean-Francois Garnier
8# C++ version (c) 2013 Christoph Gießelink
9# Python Version (c) 2015 Joachim Siebold
10#
11# This program is free software; you can redistribute it and/or
12# modify it under the terms of the GNU General Public License
13# as published by the Free Software Foundation; either version 2
14# of the License, or (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24#
25# tcpip single socket communication object class -----------------------------
26#
27# Initial version derived from ILPER 1.43
28#
29# Changelog
30# 03.02.2020 jsi:
31# - fixed Python 3.8 syntax warning
33import select
34import socket
36class SocketError(Exception):
37 def __init__(self,msg,add_msg=None):
38 self.msg = msg
39 if add_msg is None:
40 self.add_msg=""
41 else:
42 self.add_msg = add_msg
44class cls_pilsocket:
46 def __init__(self,port):
47 self.__port__=port # port for input connection
49 self.__devices__ = [] # list of virtual devices
51 self.__serverlist__ = []
52 self.__clientlist__= []
53 self.__outsocket__= None
54 self.__inconnected__= False
56 def isConnected(self):
57 return self.__inconnected__
59#
60# Connect to Network
61#
62 def open(self):
63#
64# open network connections
65#
66 host= None
67 self.__serverlist__.clear()
68 self.__clientlist__.clear()
69 for res in socket.getaddrinfo(host, self.__port__, socket.AF_UNSPEC,
70 socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
71 af, socktype, proto, canonname, sa = res
72 try:
73 s = socket.socket(af, socktype, proto)
74 except OSError as msg:
75 s = None
76 continue
77 try:
78 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
79 s.bind(sa)
80 s.listen(1)
81 self.__serverlist__.append(s)
82 except OSError as msg:
83 s.close()
84 continue
85 if len(self.__serverlist__) == 0:
86 raise SocketError("cannot bind to port","")
87#
88# Disconnect from Network
89#
90 def close(self):
91 for s in self.__clientlist__:
92 s.close()
93 for s in self.__serverlist__:
94 s.close()
96#
97# Read HP-IL frame from PIL-Box (1 byte), handle connect to server socket
98#
99 def read(self,timeout):
100 readable,writable,errored=select.select(self.__serverlist__ + self.__clientlist__,[],[],timeout)
101 for s in readable:
102 if self.__serverlist__.count(s) > 0:
103 cs,addr = s.accept()
104 self.__clientlist__.append(cs)
105 self.__inconnected__= True
106 else:
107 bytrx = s.recv(1)
108 if bytrx:
109 return (bytrx)
110 else:
111 self.__clientlist__.remove(s)
112 s.close()
113 self.__inconnected__= False
114 return None
115#
116# write bytes to the socket
117#
118 def write(self,lbyt,hbyt=None):
119 if self.__inconnected__ == False:
120 raise SocketError("cannot send data: ", " no connection")
121 if hbyt is None:
122 buf=bytearray([lbyt])
123 else:
124 buf=bytearray([lbyt,hbyt])
125 try:
126 self.__clientlist__[0].sendall(buf) ## correct ?
127 except OSError as e:
128 raise SocketError("cannot send data:",e.strerror)