File: sockets.py

package info (click to toggle)
python-sfml 1.5.1.is.1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,456 kB
  • ctags: 1,585
  • sloc: python: 5,747; cpp: 285; makefile: 147
file content (147 lines) | stat: -rw-r--r-- 4,217 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# pySFML - Python bindings for SFML
# Copyright 2012-2013, Jonathan De Wachter <dewachter.jonathan@gmail.com>
#
# This software is released under the LGPLv3 license.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sfml.network as sf

# python 2.* compatability
try: input = raw_input
except NameError: pass

def run_tcp_server():
	""" Launch a server. The server waits for an incoming connection,
	sends a message and waits for the answer. """

	try:
		# create a server socket to accept new connections
		listener = sf.TcpListener()

		# listen to the given port for incoming connections
		listener.listen(PORT)

		print("Server is listening to port {0}, waiting for connections...".format(PORT))

		# wait for a connection
		socket = listener.accept()
		print("Client connected: {0}".format(socket.remote_address))

		# send a message to the connected client
		message = "Hi, I'm the server"
		socket.send(message.encode('utf-8'))
		print("Message sent to the client: {0}".format(message))

		# recieve a message back from the client
		answer = socket.receive(128).decode('utf-8')
		print("Answer received from the client: {0}".format(answer))

	except sf.SocketException as error:
		print("An error occured!")
		print(error)
		return

def run_tcp_client():
	""" Create a client. The client is connected to a server, displays
	the welcome message and sends an answer. """

	server = input("Type the address or name of the server to connect to: ")
	server = sf.IpAddress.from_string(server)

	# create a socket for communicating with the server
	socket = sf.TcpSocket()

	# connect to the server
	try:
		socket.connect(server, PORT)
		print("Connected to server {0}".format(server))

		# receive a message from the server
		message = socket.receive(128).decode('utf-8')
		print("Message received from the server: {0}".format(message))

		# send an answer to the server
		answer = "Hi, I'm a client"
		socket.send(answer.encode('utf-8'))
		print("Message sent to the server: {0}".format(answer))

	except sf.SocketException as error:
		print("An error occured!")
		print(error)
		return

def run_udp_server():
	""" Launch a server. The server waits for a message then sends an
	answer. """

	# create a socket to receive a message from anyone
	socket = sf.UdpSocket()

	try:
		# listen to messages on the specified port
		socket.bind(PORT)
		print("Server is listening to port {0}, waiting for message...".format(PORT))

		# wait for a message
		message, ip, port = socket.receive(128)
		print("Message received from client {0}: {1}".format(ip, message.decode('utf-8')))

		# send an answer to the client
		answer = "Hi, I'm the server"
		socket.send(answer.encode('utf-8'), ip, port)
		print("Message sent to the client: {0}".format(answer))

	except sf.SocketException as error:
		print("An error occured!")
		print(error)
		return

def run_udp_client():
	""" Send a message to the server, wait for the answer. """

	# ask for the server address
	server = input("Type the address or name of the server to connect to: ")
	server = sf.IpAddress.from_string(server)

	# create a socket for communicating with the server
	socket = sf.UdpSocket()

	try:
		# send a message to the server
		message = "Hi, I'm a client"
		socket.send(message.encode('utf-8'), server, PORT)
		print("Message sent to the server: {0}".format(message))

		# receive an answer from anyone (but most likely from the server)
		answer, ip, port = socket.receive(128)
		print("Message received from {0}: {1}".format(ip, answer.decode('utf-8')))

	except sf.SocketException as error:
		print("An error occured!")
		print(error)
		return

if __name__ == "__main__":
	# choose an arbitrary port for opening sockets
	PORT = 50001

	# TCP or UDP ?
	print("Do you want to use TCP (t) or UDP (u) ?")
	protocol = input()

	# client or server ?
	print("Do you want to be a server (s) or a client (c) ?")
	who = input()

	if protocol == 't':
		if who == 's': run_tcp_server()
		else: run_tcp_client()
	else:
		if who == 's': run_udp_server()
		else: run_udp_client()

	input("Press any key to exit...")