I am developing a multiplayer game using python (pygame and podsixnet). Every time the client has to connect to the server, it has to type the server's hostname and port. I want to bypass this stage by automatically providing the client with this information. How can I find the address of my server? The game has been developed by following the tutorial:
https://www.raywenderlich.com/38732/multiplayer-game-programming-for-teens-with-python
def __init__(self):
self.justplaced=10
n = self.dialog()
print n
pygame.init()
self.clock = pygame.time.Clock()
self.screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Scrabble')
self.gameid=None
self.num=None
self.lastx, self.lasty = 0, 0
self.mousex = 0 # used to store x coordinate of mouse event
self.mousey = 0 # used to store y coordinate of mouse event
#print BGCOLOR
self.screen.fill(BGCOLOR)
#pygame.display.update()
self.is_Placed = self.generateBoxData(False)
self.placed_Letter = self.generateBoxData("")
self.is_Formed = self.generateBoxData(False)
self.owner = self.generateBoxData(99)
self.is_Clicked = self.generateBoxData(False)
address=raw_input("Address of Server: ")
try:
if not address:
host, port="localhost", 8000
else:
host,port=address.split(":")
self.Connect(("localhost" ,int(port)))
except:
print "Error Connecting to Server"
print "Usage:", "host:port"
print "e.g.", "localhost:31425"
exit()
print "Boxes client started"
I like making maltiplayer games too! :)
This is mostly what you may be looking for socket.gethostbyname(hostname)
a full small script would look like this:
import socket
hostname = 'maps.google.com'
addr = socket.gethostbyname(hostname)
print 'The address of ', hostname, 'is', addr
Hope this helps!
Related
I have created a python script to detect an ARP attack. I have already initiated a ARP spoof attack and stored the wireshark capture in a pcap file. Once the code is executed, the code is designed to alert of any possible attack based on the MAC value change.
But how do I create a dictionary in the first place to store the MAC--IP mappings, and then detect when there is a change of values to indicate an alert?
Can anyone guide me please?
from scapy.all import *
mac_table = {}
def main():
pkts = rdpcap('/root/Desktop/arp_capture.pcap')
for packet in pkts:
if packet.haslayer(ARP):
if packet[ARP].op == 2:
try:
original_mac = req_mac(packet[ARP].psrc)
new_mac = packet[ARP].hwsrc
if original_mac != new_mac:
print(f"[**] ATTACK ALERT !!!!!! CHECK ARP TABLES !!![**]")
except IndexError:
pass
def req_mac(ip):
arp_req = ARP(pdst=ip)
bcst_req = Ether(dst='ff:ff:ff:ff:ff:ff')
p = bcst_req/arp_req
result = srp(p, timeout=3, verbose=False)[0]
return result[0][1].hwsrc
if __name__ == "__main__":
main()
I have a zynq core board with its own processor , the zynq has hardcoded testfunctions which upon receiving the associated command for a function returns the data requested for example command 200 returns temperature sensor data . Using UDP in python I want to send commands to the zynq board.
Also the function will be called only upon defining the function parameters , MessageID is the command to be excuted , UniqueId and Overload is just a random number and Subgroup and SensorIndex are the inbuilt or fixed ids in the zyng for example group 0 and index 1 , this functions I have defined in another script called testfunctions def systemMonitorGetSensor(MessageID, UniqueID, Overload, Subgroup, SensorIndex):
import socket
import os
import testfunctions as test
def main(self):
def openconnection():
UDP_IP = "172.29.11.113" # Get local machine name
UDP_PORT = 4711 # Reserve a port for your service
MESSAGE = b"Hello SCE100...python talking"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
def ping():
UDP_IP = "172.29.11.113"
response = os.system('ping -c 1 ' + UDP_IP)
if response == 0:
print(UDP_IP, 'is up')
else:
print(UDP_IP, 'is down')
def stopsocket(self):
self.sock.shutdown(socket.SOCK_DGRAM)
self.sock.closes()
openconnection()
ping()
MessageID = int(input("Enter the TaskID to be executed : "))
print(MessageID)
main ( MessageID )
if MessageID == 200:
test.systemMonitorGetAllSensors(200,0,0)
if MessageID == 201:
systemMonitorResponce()
I'm trying to create a simple multiplayer game in Python using pygame and socket modules. It just consists in two circles that are controlled with the W, A, S, D keys from two different computers.
At first I created a client with the recv() in the middle of the pygame loop. It worked well but the recv() blocked the loop, so the movement of the circles wasn't smooth and I had to increase the velocity to 6 (before it was set to 0.6) in order to have a normal speed. This is the code (I've summarised it a bit):
Client 1st version
#import modules
def main(sock):
pygame.init()
#Display screen and set initial me_x and me_y
vel = 0.6
while True:
keys_pressed = pygame.key.get_pressed()
#Change me_x and me_y (with A, D, W, S keys)
#Make sure me_x and me_y don't get off the screen
screen.fill(color)
if other_x and other_y:
pygame.draw.circle(screen, colorMe, (other_x, other_y), radi)
pygame.draw.circle(screen, colorOther, (int(me_x), int(me_y)), radi)
pygame.display.flip()
sock.send(int(me_x).to_bytes(3, byteorder = 'big') + int(me_y).to_bytes(3, byteorder = 'big'))
otherPos = sock.recv(BUFSIZ)
other_x = int.from_bytes(otherPos[:3], byteorder = 'big')
other_y = int.from_bytes(otherPos[3:], byteorder = 'big')
print(other_x, other_y)
#CONNECT TO TCP SOCKET
other_x = None
other_y = None
main(client_socket)
Then, I tried to put the recv() in a thread to stop blocking the loop:
Client 2nd version
#import modules
def main(sock):
pygame.init()
#Display screen and set initial me_x and me_y
vel = 0.6
while True:
for i in range(30):
keys_pressed = pygame.key.get_pressed()
#Change me_x and me_y (with A, D, W, S keys)
#Make sure me_x and me_y don't get off the screen
screen.fill(color)
if other_x and other_y:
pygame.draw.circle(screen, colorOther, (other_x, other_y), 15)
pygame.draw.circle(screen, colorMe, (int(me_x), int(me_y)), 15)
pygame.display.flip()
msg = int(me_x).to_bytes(3, byteorder = 'big') + int(me_y).to_bytes(3, byteorder = 'big')
sock.send(msg)
def recv_pos(sock):
while True:
other_pos = sock.recv(BUFSIZ)
other_x = int.from_bytes(other_pos[:3], byteorder = 'big')
other_y = int.from_bytes(other_pos[3:], byteorder = 'big')
print(other_x, other_y)
#CONNECT TO TCP SOCKET
other_x = None
other_y = None
receive_thread = threading.Thread(target = recv_pos, args = (client_socket,))
receive_thread.daemon = True
receive_thread.start()
main(client_socket)
However, when I start 2 instances of the client 2 in two different computers, it gives me an OverflowError:
OverflowError: Python int too large to convert to C long
I added the for i in range(30): because I thought that the server was getting collapsed because there were too much messages being sent at the same time. The output is the same: after 3 seconds more or less, the program crashes giving an OverflowError.
I added a print() statement in both versions just after the recv() to see which values of x and y I was receiving. In the 1st version, all of them were in the width and height range. However, in the 2nd version, 1/5 of the received messages was a big number such as 124381473265. If this number is even bigger, it gives the OverflowError. I don't understand why this is happening: I'm encoding and decoding the messages in the same way in both versions, but one works and the other doesn't.
I'm not including the server code because I don't think it's necessary. It's just just transmits the messages between both clients without modifying them. The server is not the error, as in the 1st client version the messages are sent and received properly.
Any help will be apprecieted. If you need more information about any point just tell me.
Thanks in advance!
Put your socket code into a thread, or use select.select() for non-blocking socket-reads. Then post custom event-messages back to the main loop when a data-gram arrives from the server.
import pygame
import enum
class NetworkEvents( enum.IntEnum ):
EVENT_HANGUP = pygame.USEREVENT + 1
EVENT_MESSAGE = pygame.USEREVENT + 2
Your socket-reading code should handle partial-packets, hang-ups and delays too. I think your error above is caused by trying to unpack a partial (or somehow junk) packet. Commonly people use the pickle module to encapsulate this data, but to begin with, I would do pre-testing with simple string data. It's easier to debug. Then once the transmission code is all bedded-down and tested, change to binary - if necessary.
I like to use select() in my socket code no matter what. It gives fine-grained control over what's happening with the socket.
Here's a simple threaded socket-conversation handler. It really only supports socket-hangup and incoming message. But it could be easily extended to handle further message types.
import threading
import pygame
import random
import enum
import socket
import select
import time
class ConversationHandlerThread( threading.Thread ):
""" A thread that handles a conversation with a single remote server.
Accepts commands of 'close', 'red', 'green' or 'blue', and posts messages
to the main PyGame thread for processing """
def __init__( self, server_address, server_port ):
threading.Thread.__init__(self)
self.server_address = server_address
self.server_port = server_port
self.server_socket = None
self.data_buffer = ''
self.daemon = True # exit with parent
self.done = False
def stop( self ):
self.done = True
def connect( self ):
self.server_socket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
while True:
try:
self.server_socket.connect( ( self.server_address, self.server_port ) )
print( "Connected to %s:%d" % ( self.server_address, self.server_port ) )
break;
except:
print( "Failed to connect %s:%d" % ( self.server_address, self.server_port ) )
time.sleep( 12 )
print( "Retrying..." )
def run( self ):
""" Connects to Server, then Loops until the server hangs-up """
self.connect()
# Now we're connected, start reading commands
read_events_on = [ self.server_socket ]
while ( not self.done ):
# Wait for incoming data, or errors, or 0.3 seconds
(read_list, write_list, except_list) = select.select( read_events_on, [], [], 0.5 )
if ( len( read_list ) > 0 ):
# New data arrived, read it
incoming = self.server_socket.recv( 8192 )
if ( len(incoming) == 0):
# Socket has closed
new_event = pygame.event.Event( NetworkEvents.EVENT_HANGUP, { "address" : self.server_address } )
pygame.event.post( new_event )
self.server_socket.close()
self.done = True
else:
# Data has arrived
try:
new_str = incoming.decode('utf-8')
self.data_buffer += new_str
except:
pass # don't understand buffer
# Parse incoming message (trivial parser, not high quality)
# commands are '\n' separated
if (self.data_buffer.find('\n') != -1 ):
for line in self.data_buffer.split('\n'):
line = line.strip()
# client disconnect command
if ( line == 'close' ):
new_event = pygame.event.Event( NetworkEvents.EVENT_HANGUP, { "address" : self.server_address } )
pygame.event.post( new_event )
self.server_socket.close()
self.done = True
# only make events for valid commands
elif ( line in ( 'red', 'green', 'blue' ) ):
new_event = pygame.event.Event( NetworkEvents.EVENT_MESSAGE, { "address" : self.server_address, "message" : line } )
pygame.event.post( new_event )
self.data_buffer = '' # all used-up
The beauty of putting all this complexity into a thread, is that once it's started, you can forget about it.
# Start the network-handler thread
thread1 = ConversationHandlerThread( '127.0.0.1', 5555 )
thread1.start()
In your main loop, process the events like any others:
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == NetworkEvents.EVENT_HANGUP ):
print(" CLIENT DISCONNECTED %s " % ( str(event.address) ) )
elif ( event.type == NetworkEvents.EVENT_MESSAGE ):
print(" CLIENT MESSAGE FROM %s - %s " % ( str(event.address), event.message ) )
if ( event.message == 'red' ):
new_sprite = AlienSprite( RED )
SPRITES.add( new_sprite )
i have to read some data from database and send it from a tcp socket
so i fetch data from database
#main
while True:
cursor.execute("UPDATE `raw` SET `zombie`='"+zombieId+"' WHERE result='pending' AND protocol='tcp' AND zombie='0' LIMIT 1;")
# time.sleep(0.2)
cursor.execute("select * from raw WHERE `result`='pending' AND `protocol`='tcp' and `zombie`='"+zombieId+"' limit 1;")
if cursor.rowcount>0 :
waitedSig = cursor.fetchone()
time.sleep(0.2)
t = threading.Thread(target=sendData , args=((waitedSig),))
t.start()
time.sleep(0.6)
else:
time.sleep(1)
on the thread i will send data to target
def sendData(sig):
timedata = datetime.datetime.fromtimestamp(int(sig[16]))
devimei = sig[23]
devdate = timedata.strftime("%d%m%y")
devtime = timedata.strftime("%H%M%S")
lat= format(sig[2])
lon= format(sig[3])
satcount = format(sig[5])
speed = format(sig[4])
batery = format(sig[7])
if sig[9]>1000:
band='00'
elif sig[9]>850:
band='02'
else:
band='01'
hdop = format(sig[10])
gsmQ = format(sig[6])
lac = format(sig[12])
cid = format(sig[13])
str='$MGV002,'+devimei+',12345,S,'+devdate+','+devtime+',A,'+lat+',N,'+lon+',E,0,'+satcount+',00,'+hdop+','+speed+',0,,,432,11,'+lac+','
try:
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = clientsocket.connect(('ip',port))
clientsocket.send(str)
data = clientsocket.recv(1024)
print(str(datetime.datetime.now())+' -> send completed :'+format(sig[0]))
clientsocket.close()
except:
print(str(datetime.datetime.now())+' -> connection to tcp server failed!!')
this will work really good but there are two boring problem:
1) if i remove 0.2 and 0.6 sleep delay the script crash due to duplicate socket usage,it seems system try to open an other socket until the previous don't finished its job yet!
2) if some thing goes wrong in the sendData function,the whole script stop working until i manually restart the script
so
1) can i create a thread queue to run one after other and don't affect each other?!
2) how can i handle errors in the thread function to close just that specific thread and script continue its work with next database record?!
This looks like a good application of a thread pool. In your implementation you create one thread and socket per item in your database table, and that could tax the system extremely. Here I've created 20 workers as an example. There are diminishing returns on the number of workers as you start to stress the system.
import multiprocessing.pool
def sender():
pool = multiprocessing.pool.ThreadPool(20) # pick your size...
cursor.execute("select * from database")
pool.map(sendData, cursor, chunksize=1)
def sendData(sig):
try:
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = clientsocket.connect(('ip',port))
clientsocket.sendall(sig)
data = clientsocket.recv(1024)
print(str(datetime.datetime.now())+' -> send completed :'+format(sig[0]))
clientsocket.shutdown(socket.SOCK_RDWR)
clientsocket.close()
except:
print(str(datetime.datetime.now())+' -> connection to tcp server fa
I need to built a python chat and I'm stacked in the very final step. I've built the server and the client and I have the following problem while running the code:
server.py 127.0.0.1
-in a separate window client.py 127.0.0.1
-another client
-type the nicknames to chat for both clients and get the correct answer 'yuppie' meaning you are connected
a client try to speak
message is not read by the other client until it doesn't print something, after printing it get the message printed on its screen correctly.
I'd like to get the message without being obliged to print something, it's pretty unrealistic!!! Code of client and server are below in 2 different classes. Thank you!
#! /usr/bin/env python
import socket,sys,select,re
PORT=1060
class Server():
def __init__(self,host):
#building listen_sock
self.listen_sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.listen_sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
self.listen_sock.bind((host,PORT))
self.listen_sock.listen(20)
#building dict for socket and socket state
self.sockets={self.listen_sock.fileno(): self.listen_sock}
self.socket_state={self.listen_sock.fileno():''}
#building poll object
self.poll=select.poll()
self.poll.register(self.listen_sock,select.POLLIN)
#users' list
self.users_list={}
#DON'T LOOK HERE
#initialize the sender
#self.sender=0
# self.users=re.compile("\s*\$(get users connected)$\s*",re.IGNORECASE)
# self.nick=re.compile("\s*\$\$(\w*\d*)\$\$\s*",re.IGNORECASE)
# self.quit=re.compile("\s*\$(quit)\$\s*",re.IGNORECASE)
#self.commands=[self.users,self.nick,self.quit]
#funcion to receive message from client (work well)
def recv_until(self,fd,suffix):
self.message=''
#checking the end of the message
while not self.message.endswith(suffix):
data=self.sockets[fd].recv(16)
if not data:
raise EOFError('socket closed before we saw %r' % suffix)
self.message+=data
self.message=self.message[:-1]
#delete client (work well)
def del_client(self,fd):
del self.users_list[fd]
del self.socket_state[fd]
self.poll.unregister(fd)
#print the remaining active connections
if not len(self.users_list):
print 'Anyone is connected, waiting for new connection'
else:
print self.users_list
#add new client and change the of the file descriptor for that client (work well)
def new_client(self,fd):
newsock, sockname = self.listen_sock.accept()
print 'new connection from ', newsock.getpeername()
newsock.setblocking(False)
#recording the new connection
fd=newsock.fileno()
self.sockets[fd]=newsock
self.poll.register(fd,select.POLLOUT)
self.socket_state[fd]='ask nick'
#DON'T LOOK HERE
# def handle_query(self,fd):
# for n,command in enumerate(self.commands):
# match=command.search(self.message)
# if n==1 and match:
# self.users_list[self.sockets[fd].getpeername()]=match.group(1)
# print self.users_list
# for value in self.users_list.values():
# self.sockets[fd].sendall(value+'\n')
#starting the main function of the class
def chat(self):
while True:
#here il where the code hangs up waitng and waiting (WORKS BAD)
#return a tuple, identify where (fd) the event (event) is happening
for fd,event in self.poll.poll():
#print the state of each socket and the poll object
print self.socket_state
print self.poll.poll()
#starting the state machine
#remove closed sockets
if event & (select.POLLHUP | select.POLLERR |
select.POLLNVAL):
#deleting the socket closed at fd
self.del_client(fd)
#if the socket referred to is our listen_sock and we have a new connection request
elif self.sockets[fd] is self.listen_sock:
#recording the new entry!
self.new_client(fd)
#managing all the situation where it is necessary to answer to a client
#and changing the state of the socket and that of the sockets[fd]
elif event & select.POLLOUT:
if self.socket_state[fd]=='ask nick':
self.sockets[fd].sendall('identify\n')
self.poll.modify(self.sockets[fd],select.POLLIN)
self.socket_state[fd]='get user'
if self.socket_state[fd]=='invalid nick':
self.sockets[fd].sendall('invalid nick\n')
for value in self.users_list.values():
self.sockets[fd].sendall('\n'+value+'\n')
self.socket_state[fd]='ask nick'
if self.socket_state[fd]=='connected':
print '3'
self.sockets[fd].sendall('yuppie\n')
self.poll.modify(self.sockets[fd],select.POLLIN)
self.socket_state[fd]='ready to communicate'
if self.socket_state[fd]=='ready to receive':
self.sockets[fd].sendall(self.message)
print '4'
self.poll.modify(self.sockets[fd],select.POLLIN)
self.socket_state[fd]='ready to communicate'
#managing all the situation where it is necessary to get values from clients
elif event & select.POLLIN:
if self.socket_state[fd]=='get user':
self.recv_until(fd,'\n')
if self.message not in self.users_list.values():
self.users_list[fd]=self.message
self.poll.modify(self.sockets[fd],select.POLLOUT)
self.socket_state[fd]='connected'
else:
self.poll.modify(self.sockets[fd],select.POLLOUT)
self.socket_state[fd]='invalid nick'
if self.socket_state[fd]=='ready to communicate':
self.recv_until(fd,'\n')
print '5'
for i in self.users_list.keys():
if i!=fd:
self.poll.modify(self.sockets[i],select.POLLOUT)
self.socket_state[i]='ready to receive'
if __name__ == '__main__':
se=Server(sys.argv[1])
se.chat()
#! /usr/bin/env python
import sys,socket,select,threading,time
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST=sys.argv.pop()
PORT=1060
class Client():
def setup(self):
server_address=(HOST,PORT)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(server_address)
def chat(self):
while True:
time.sleep(1)
text=raw_input('>>> ')
self.sock.sendall(text+'\n')
def rec(self):
while True:
mess=self.sock.recv(16)
if mess:
print '$$$ ', mess,
def start(self):
l=threading.Thread(target=self.rec)
t=threading.Thread(target=self.chat)
t.start()
l.start()
if __name__=='__main__':
cl=Client()
cl.setup()
cl.start()
Next time take a look at http://www.zeromq.org/, it has a nice python binding http://zeromq.github.com/pyzmq/. It's perfect for this kind of stuff.